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

Nick Coghlan ncoghlan at gmail.com
Mon Jan 29 23:53:51 EST 2018


On 30 January 2018 at 01:43, David Mertz <mertz at gnosis.cx> wrote:
> Nick suggests:
>     >>> print(f"In European format x is {x:,.2f}, in Indian format it
> is {x:,2,3.2f}")
>
> This looks very good and general.  I only know of the "European" and South
> Asian conventions in widespread use, but we could give other grouping
> conventions using that little syntax and it definitely covers the ones I
> know about.  There's not an issue about this giving the parser for the
> format mini-language hiccups over width specifier in there, is there?

That's the part I haven't explicitly checked in the code, but I think
it would be feasible based on
https://docs.python.org/3/library/string.html#format-specification-mini-language

My proposal is essentially to replace the current:

     grouping_option ::=  "_" | ","

with:

     grouping_option ::=  underscore_grouping | comma_grouping
     underscore_grouping ::= "_" [group_width ("_" group_width)*]
     comma_grouping ::= "," [group_width ("," group_width)*]
     group_width ::= digit+

That's unambiguous, since the grouping field still always starts with
"_" or ",", and the next field must be either the precision (which
always starts with "."), the type (which is always a letter, and never
a number or symbol), or the closing brace for the field specifier.

Cheers,
Nick.


P.S. While writing this I noticed that the current format
mini-language docs are incorrect and say "integer" where they should
be saying "digit+": https://bugs.python.org/issue32720

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


More information about the Python-ideas mailing list