format a number for output

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Mon Aug 7 11:11:43 EDT 2006


In <1154962511.485221.229830 at i3g2000cwc.googlegroups.com>, abcd wrote:

> if i have a number, say the size of a file, is there an easy way to
> output it so that it includes commas?
> 
> for example:
> 
> 1890284
> 
> would be:
> 
> 1,890,284

I think this comes close:

In [23]: import locale

In [24]: locale.setlocale(locale.LC_ALL, '')
Out[24]: 'en_US.UTF-8'

In [25]: locale.format('%d', 1890284, True)
Out[25]: '1,890,284'

It's not always commas but whatever character is used to group thousands
in the active locale.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list