format a number for output

Paul Rubin http
Mon Aug 7 16:38:25 EDT 2006


"abcd" <codecraig at gmail.com> writes:
> 1890284
> 
> would be:
> 
> 1,890,284

"To iterate is human; to recurse, divine":

    def commafy(n):
       if n < 0: return '-' + commafy(-n)
       if n >= 1000: return '%s,%03d' % (commafy(n//1000), n % 1000)
       return '%s'% n

I don't like the locale solution because of how messy locales are.



More information about the Python-list mailing list