formatting a string with thousands separators

S

SimonPalmer

anyone recommend a way of formatting floats with comma separators?

e.g. 500000.00 -> 500,000.00
 
F

Fredrik Lundh

James said:
There is a much easier more consistent way:

'en_AU.UTF-8'

doesn't work on all Python platforms, though:
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top