formatting a string with thousands separators

Fredrik Lundh fredrik at pythonware.com
Sun Sep 7 18:01:01 EDT 2008


James Mills wrote:

> There is a much easier more consistent way:
> 
>>>> import locale
>>>> locale.setlocale(locale.LC_ALL, "en_AU.UTF-8")
> 'en_AU.UTF-8'

doesn't work on all Python platforms, though:

 >>> locale.setlocale(locale.LC_ALL, "en_AU.UTF-8")
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "c:\python25\lib\locale.py", line 478, in setlocale
     return _setlocale(category, locale)
locale.Error: unsupported locale setting

and is likely to give you somewhat unpredictable output if you use the 
machine's actual locale:

 >>> import os
 >>> locale.setlocale(locale.LC_ALL, os.environ["LANG"])
'sv_SE.UTF-8'
 >>> locale.format("%0.2f", 5000000, True)
'5000000,00'

</F>




More information about the Python-list mailing list