Rough draft: Proposed format specifier for a thousands separator
MRAB
google at mrabarnett.plus.com
Mon Mar 16 19:04:58 EDT 2009
Rhodri James wrote:
> On Mon, 16 Mar 2009 02:36:43 -0000, MRAB <google at mrabarnett.plus.com>
> wrote:
>
>> The field name can be an integer or an identifier, so the locale could
>> be too, provided that you know where to look it up!
>>
>> financial = Locale(group_sep=",", grouping=[3])
>> print("my number is {0:10n:{fin}}".format(1234567, fin=financial))
>>
>> Then again, shouldn't that be:
>>
>> fin = Locale(group_sep=",", grouping=[3])
>> print("my number is {0:{fin}}".format(1234567, fin=financial))
>
> Except that loses you the format, since the locale itself is a collection
> of parameters the format uses. The locale knows how to do groupings, but
> not whether to do them, nor what the field width should be. Come to think
> of it, it doesn't know whether to use the LC_NUMERIC grouping information
> or the LC_MONETARY grouping information. Hmm.
>
> I can't believe I'm even suggesting this, but how about:
>
> print("my number is {fin.format("10d", {0}, True)}".format(1235467,
> fin=financial))
>
> assuming the locale.format() method remains unchanged? That's horrible,
> and I'm pretty sure it can't be right, but I'm too tired to think of
> anything more sensible right now.
>
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.
More information about the Python-list
mailing list