Formatting numbers

Siggy Brentrup bsb at winnegan.de
Tue May 1 20:02:25 EDT 2001


"Daniel Klein" <DanielK at jBASE.com> writes:

> This is almost embarrasing to have to ask this but I can't seem to find this
> information.
> 
> Given an integer, 12345, how to format this to 123.45 ?

def fixed(i, places=2):
    """12345 -> 123.45"""
    if i >= 0:
        s = '%0*d' % (places+1,i)
    else:
        s = '%0*d' % (places+2,i)
    return s[:-places]+'.'+s[-places:]

>>> print fixed(12345), fixed(12345,6), fixed(-1)
123.45 0.012345 -0.01

HTH
  Siggy
-- 
Siggy Brentrup - bsb at winnegan.de - http://www.winnegan.de/
****** ceterum censeo javascriptum esse restrictam *******





More information about the Python-list mailing list