[Tutor] string formatting currency

Kent Johnson kent37 at tds.net
Tue Jun 20 20:13:23 CEST 2006


Paul D. Kraus wrote:
> How can i print a float as a currency with commas.
> 
> 1222333.4 -> 1,222,333.40
> 
> if i '%0.2f'%number
> 
> i get
> 1222333.40

Use locale.format():

In [1]: import locale

In [2]: f=1222333.4

In [6]: locale.setlocale(locale.LC_ALL, "")
Out[6]: 'English_United States.1252'

In [7]: locale.format('%.2f', f, True)
Out[7]: '1,222,333.40'

Kent



More information about the Tutor mailing list