Most pythonic way to format a number with commas?

Emile van Sebille emile at fenx.com
Thu Jul 11 20:51:29 EDT 2002


Ned Batchelder
> For dir/ls replacement script, I wanted to format file sizes with
> commas as a thousands separator.  I didn't find anything in the
> standard library, 
[snip]
> Is there one?

It's buried in locale...

>>> import locale
>>> locale.setlocale(locale.LC_ALL, "")
'English_United States.1252'
>>> locale.format("%8.2f", 1234.56)
' 1234.56'
>>> locale.format("%8.2f", 1234.56,3)
'1,234.56'

--

Emile van Sebille
emile at fenx.com

---------




More information about the Python-list mailing list