[Python-ideas] Format mini-language for lakh and crore

Nick Coghlan ncoghlan at gmail.com
Wed Jan 31 23:11:25 EST 2018


On 1 February 2018 at 08:14, Eric V. Smith <eric at trueblade.com> wrote:
> On 1/29/2018 2:13 AM, Nick Coghlan wrote:
>> Given the example, I think a more useful approach would be to allow an
>> optional digit grouping specifier after the comma separator, and allow
>> the separator to be repeated to indicate non-uniform groupings in the
>> lower order digits.
>>
>> If we did that, then David's example could become:
>>
>>      >>> print(f"In European format x is {x:,.2f}, in Indian format it
>> is {x:,2,3.2f}")
>
>
> This just seems too complicated to me, and is overgeneralizing. How many of
> these different formats would ever really be used? Can you really expect
> someone to remember what that means by looking at it?

Sure - "," and "_" both mean "digit grouping", the numbers tell you
how large the groups are from left to right (with the leftmost group
size repeated as needed), and a single "," means the same thing as
",3," for decimal digits, and the same thing as ",4," for binary,
octal, and hexadecimal digits.

Another advantage of this approach is that we'd be able to control the
grouping of binary and hexadecimal numbers printed with "_", rather
than the current approach where we're restricted to half-byte grouping
for binary and 16-bit word grouping for hex.

> If you are going to generalize it, at least go all the way and support the
> struct lconv "CHAR_MAX" behavior, too.

I'm not sure how common that convention is, but if we wanted to
support it then I'd spell it by repeating the group separator without
an intervening group size specifier:

    {x:,,2,3.2f} # Ungrouped leading digits, single group of 2, single
group of 3

> However, I suggest just pick another character to use instead of ",", and
> have it mean the 2,3 format. With no evidence (and willing to be wrong), it
> seems like it's the next-most needed variety of this. Maybe use ";"?

That's even more arbitrary and hard to interpret than listing out the
grouping spec, though.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list