Can Python format long integer 123456789 to 12,3456,789 ?

Ben Cartwright bencvt at gmail.com
Thu Jun 1 20:11:16 EDT 2006


A.M wrote:
> Is there any built in feature in Python that can format long integer
> 123456789 to 12,3456,789 ?

The locale module can help you here:

  >>> import locale
  >>> locale.setlocale(locale.LC_ALL, '')
  'English_United States.1252'
  >>> locale.format('%d', 123456789, True)
  '123,456,789'

Be sure to read the caveats for setlocale in the module docs:
  http://docs.python.org/lib/node323.html
I'd recommend calling setlocale only once, and always at the start of
your program.

--Ben




More information about the Python-list mailing list