format a number for output

Yu-Xi Lim yuxi at ece.gatech.edu
Mon Aug 7 15:10:27 EDT 2006


Tim Williams wrote:
>>>> a = 1890284
>>>> ','.join([str(a)[::-1][x:x+3] for x in range(len(str(a)))[::3]])[::-1]
> '1,890,284'
> 
> Ugly !
> 

 >>> b = 189028499
 >>> ','.join([str(b)[::-1][x:x+3] for x in range(len(str(b)))[::3]])[::-1]

'-,189,028,499'

 >>> c = 1890284.1
 >>> ','.join([str(c)[::-1][x:x+3] for x in range(len(str(c)))[::3]])[::-1]

'189,028,4.1'


Stick to using locale. :)



More information about the Python-list mailing list