Insert comma in number?

Peter Otten __peter__ at web.de
Thu Mar 7 08:55:43 CET 2013


eli m wrote:

> I have a python program that accepts input and calculates the factorial of
> that number, and i want to know if i can make it so commas get inserted in
> the number. For example: instead of 1000 it would say 1,000

Last not least there's the option to employ locale-aware formatting:

>>> import locale
>>> locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
'en_US.UTF-8'
>>> locale.format("%d", 12345, grouping=True)
'12,345'

In German usage of "." and "," is reversed, so:
 
>>> locale.setlocale(locale.LC_ALL, "de_DE.UTF-8")
'de_DE.UTF-8'
>>> locale.format("%d", 12345, grouping=True)
'12.345'





More information about the Python-list mailing list