Formate a number with commas

Chris Rebert clp2 at rebertia.com
Thu Feb 9 17:12:35 EST 2012


On Thu, Feb 9, 2012 at 1:16 PM, Peter Otten <__peter__ at web.de> wrote:
> Chris Rebert wrote:
>> On Thu, Feb 9, 2012 at 12:39 PM, Peter Otten <__peter__ at web.de> wrote:
>>>>>> import locale
>>>>>> locale.setlocale(locale.LC_ALL, "")
>>> 'de_DE.UTF-8'
>>>>>> "{:n}".format(1234) # locale-aware
>>> '1.234'
>>>>>> "{:,d}".format(1234) # always a comma
>>> '1,234'
>>
>> The latter requires Python 3.1+ and is courtesy PEP 378
>> (http://www.python.org/dev/peps/pep-0378/ ).
>
> I actually ran the above session in 2.7, so that should do, too.
>
> http://docs.python.org/whatsnew/2.7.html#pep-378-format-specifier-for-
> thousands-separator

Argh. The 2.7 docs say it was added in 2.7, but the 3.3a0 docs say it
was added in 3.1 (Guido's backporting time machine messes with
"causality").
Both statements are completely technically correct, but misleading
when not taken together.

Cheers,
Chris



More information about the Python-list mailing list