format a number for output

BartlebyScrivener rpdooling at gmail.com
Mon Aug 7 17:32:52 EDT 2006


Paul Rubin wrote:

> "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.

That's a keeper!

Thanks!

rd




More information about the Python-list mailing list