Formatting question : printing numbers with thousands separators ?

Fred Pacquier fredp at mygale.org.nospam
Fri Aug 31 10:47:20 EDT 2001


gerson.kurz at t-online.de (Gerson Kurz) said :

> On 31 Aug 2001 11:33:52 GMT, Fred Pacquier <fredp at mygale.org.nospam>
> wrote:
> 
>>how do I print 246814345 as 246,814,345 (or 246 814 345) ?
> 
> def f(x):
>     s,i = list(str(x)),3
>     while i < len(s):
>         s.insert(len(s)-i,',')
>         i += 4
>     return ''.join(s)
 
Well, before rolling my own buggy routine I wanted to check I wasn't 
overlooking something ready-made "à la sprintf". Turns out I wasn't looking 
in the right place -- as thousands separators are country-dependent, the 
"locale" module is the way to go...
Thanks,
fp
-- 
YAFAP : http://www.multimania.com/fredp/



More information about the Python-list mailing list