Formatting numbers
Andrew Henshaw
andrew_dot_henshaw_at_earthling_dot_net
Wed May 2 00:55:01 EDT 2001
"Steve Holden" <sholden at holdenweb.com> wrote in message
news:%4KH6.58717$qc2.15144507 at typhoon.southeast.rr.com...
...snip...
>
> so this won't be useful in the general case. One way might be:
>
> >>> def fmt(i):
> ... bits = divmod(i, 100)
> ... return "%d.%02d" % bits
> ...
> >>> fmt(12345)
> '123.45'
> >>> fmt(12340)
> '123.40'
> >>> fmt(12300)
> '123.00'
>
...snip...
This approach won't work correctly for negative values.
>>> fmt(-12345)
'-124.55'
Andy Henshaw
More information about the Python-list
mailing list