[Python-ideas] Rough draft: Proposed format specifier for a thousands separator (discussion moved from python-dev)

Eric Smith eric at trueblade.com
Fri Mar 13 12:35:44 CET 2009

Antoine Pitrou wrote:
> Greg Ewing <greg.ewing at ...> writes:
>> My point is that while it's perfectly reasonable
>> for, e.g. a French programmer to want to format his
>> numbers with dots and commas the other way around,
>> it's *not* reasonable to force him to tediously specify
>> it in each and every format specifier he writes.
> A program often formatting numbers the same way can factor that into dedicated
> helpers:
>     def format_float(f):
>         return "{0:T.,2f}".format(f)
> or even:
>     format_float = "{0:T.,2f}".format

float_fmt = 'T.,2f'

then you can re-use it everywhere, and multiple times in a single 
.format() expression:
'{0:{fmt}} {1:{fmt}}.format(3.14, 2.72, fmt=float_fmt)

(Try that with %-formatting! :-)

Or with a slight modification to the work I'm doing to implement 
'{:{fmt}} {:{fmt}}'.format(3.14, 2.78, fmt=float_fmt)
(but this is a different issue!)


More information about the Python-ideas mailing list