Number Format function

Rick Zantow rzantow at gmail.com
Wed Feb 8 16:59:04 EST 2006


"wittempj at hotmail.com" <martin.witte at gmail.com> wrote in 
news:1139433369.390308.170070 at f14g2000cwb.googlegroups.com:

> def number_format(num, places=0):
>     """Format a number according to locality and given places"""
>     locale.setlocale(locale.LC_ALL, "")
>     return locale.format("%.*f", (places, num), True)
> 

There are some edge cases in the format conversion that could present 
some issues. For example:

>>> print number_format( 2312753.44500000, 2 )
2,312,753.44
>>> print number_format( 2312753.44500001, 2 )
2,312,753.45

I would expect the first to produce the same results as the second, but, 
I suppose because of one of floating point's features, it doesn't work 
that way.

-- 
rzed



More information about the Python-list mailing list