Rough draft: Proposed format specifier for a thousands separator

MRAB google at mrabarnett.plus.com
Tue Mar 17 02:47:32 CET 2009


Rhodri James wrote:
> On Mon, 16 Mar 2009 23:04:58 -0000, MRAB <google at mrabarnett.plus.com> 
> wrote:
> 
>> It should probably(?) be:
>>
>>      financial = Locale(group_sep=",", grouping=[3])
>>      print("my number is {0:10n:fin}".format(1234567, fin=financial))
>>
>> The format "10n" says whether to use separators or a decimal point; the
>> locale "fin" says what the separator and the decimal point look like.
> 
> That works, and isn't an abomination on the face of the existing 
> syntax.  Excellent.
> 
> I'm rather presuming that the "n" presentation type does grouping.  I've 
> only got Python 2.5 here, so I can't check it out (no str.format() 
> method and "%n" isn't supported by "%" formatting).  If it does, an "m" 
> type to do the same thing only with the LC_MONETARY group settings 
> instead of the LC_NUMERIC ones would be a good idea.
> 
> This would be my preferred solution to Raymond's original 
> comma-in-the-format-string proposal, by the way: add an "m" presentation 
> type as above, and tell people to override the LC_MONETARY group 
> settings in the global locale.  It's clear that it's a bodge, and 
> weaning users onto local locales (!) wouldn't be so hard later on.
> 
> Anyway, time I stopped hypothesising about locales and started looking 
> at the actual code-base, methinks.
> 
I'm not against putting a comma in the format to indicate that grouping
should be used just as a dot indicates that a decimal point should be
used. The locale would say what characters would be used for them.

I would prefer the format to have a fixed default so that if you don't
specify the locale the result is predictable.



More information about the Python-list mailing list