Formatting question : printing numbers with thousands separators ?

Alex Martelli aleax at aleax.it
Fri Aug 31 10:47:33 EDT 2001


"Skip Montanaro" <skip at pobox.com> wrote in message
news:mailman.999263627.3276.python-list at python.org...
>
>     Fred> how do I print 246814345 as 246,814,345 (or 246 814 345) ?
>
> I'm sure there are more clever ways, but this seems to work:

Sorry for "hijacking" your response skip, but I didn't get
Fred's original question!  So -- the locale module is the
one that serves this need:

>>> locale.setlocale(locale.LC_NUMERIC, 'us')
'English_United States.1252'
>>> locale.format("%d",1024*1024,1)
'1,048,576'
>>>

Unfortunately, this implies a global, non-thread-safe
change, and is NOT oriented to just letting one format
a number once with a given thousand-separator, so it
may or may not be what one was looking for.


Alex







More information about the Python-list mailing list