Rick Zantow <[EMAIL PROTECTED]> wrote:
print number_format( 2312753.4450, 2 )
>2,312,753.44
print number_format( 2312753.4451, 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
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote in
news:[EMAIL PROTECTED]:
> 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
This is a little faster:
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)
I tested this ok with my test
--
http://mail.python.org/mailman/listinfo/pyth
Thanks. I noticed the bugs later. But after talking with my boss, he
suggested something more elegant (again *untested*, yet):
import locale
def number_format(num, places=0)
"""Format a number according to locality and given places"""
locale.setlocale(locale.LC_ALL, locale.getdefaultloc
Your code has a little bug, I highly recommend to add a test to your
code, for an idea see below - I fixed your code as well.
#!/usr/bin/env python
import math
def number_format(num, places=0):
"""Format a number with grouped thousands and given decimal
places"""
#is_negative = (num < 0)
I'm am relatively new to Python but use it daily. Today, I went looking
for a function, like PHP's number_function, that will take a number and
return a string with number formatted with grouped thousands and the
decimal portion rounded to a given number of places. This is certainly
needed wh