Formatting question : printing numbers with thousands separators ?

Fred Pacquier fredp at mygale.org.nospam
Fri Aug 31 11:02:29 EDT 2001


Skip Montanaro <skip at pobox.com> said :

> I'm sure there are more clever ways, but this seems to work:
> 
>     def separate(n, sep=','):
>         ln = list(str(n))
>         ln.reverse()
>         newn = []
>         while len(ln) > 3:
>             newn.extend(ln[:3])
>             newn.append(sep)
>             ln = ln[3:]
>         newn.extend(ln)
>         newn.reverse()
>         return "".join(newn)

Thanks Skip -- turns out the question wasn't so simple after all, and 
TIMTOWTDI :-)
Rolling your own function means you understand what's going on -- Emile's 
"locale" trick is more like black magic, but means leed typing :-)

-- 
YAFAP : http://www.multimania.com/fredp/



More information about the Python-list mailing list