[Tutor] string formatting currency

Python python at venix.com
Tue Jun 20 20:47:05 CEST 2006


On Tue, 2006-06-20 at 13:49 -0400, 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

import locale

>>> locale.setlocale(locale.LC_ALL,"")
'en_US.UTF-8'
>>> currency_symbol = locale.localeconv()['currency_symbol']
>>> num = 1222333.4
>>> print currency_symbol + locale.format("%.2f", num, 1)  # insert commas
$1,222,333.40

> 
> Thanks,
> Paul
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
-- 
Lloyd Kvam
Venix Corp



More information about the Tutor mailing list