Formatting floats

Dan Bishop danb_83 at yahoo.com
Thu Dec 11 21:49:22 EST 2003


Kevin Smith <Kevin.Smith at sas.com> wrote in message news:<20031211143214964-0500 at braeburn.themorgue.org>...
> I'm trying to get floats formatted in exponent notation but with the 
> first non-zero number to the right of the decimal instead of to the left (
> e.g. '-.107E2' instead of '-1.07E1').  Is this possible using the string 
> formatting operations?

>>> def formatFloat(x):
...    mantissa, exponent = ('%e' % x).split('e')
...    mantissa = float(mantissa) / 10
...    exponent = int(exponent) + 1
...    return '%fe%d' % (mantissa, exponent)
...
>>> formatFloat(-1.07E1)
'-0.107000e2'




More information about the Python-list mailing list