Currency formatting in Python


Question

I am looking to format a number like 188518982.18 to £188,518,982.18 using Python.

How can I do this?

1
136
9/27/2016 4:28:59 PM

Accepted Answer

See the locale module.

This does currency (and date) formatting.

>>> import locale
>>> locale.setlocale( locale.LC_ALL, '' )
'English_United States.1252'
>>> locale.currency( 188518982.18 )
'$188518982.18'
>>> locale.currency( 188518982.18, grouping=True )
'$188,518,982.18'
183
5/20/2014 4:51:01 PM

New in 2.7

>>> '{:20,.2f}'.format(18446744073709551616.0)
'18,446,744,073,709,551,616.00'

http://docs.python.org/dev/whatsnew/2.7.html#pep-0378


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon