Converting 5.223701009526849e-05 to 5e-05

Alexander Blinne news at blinne.net
Thu May 7 04:00:51 EDT 2015


Am 03.05.2015 um 10:48 schrieb Ben Finney:
> That's not as clear as it could be. Better is to be explicit about
> choosing “exponential” format::
> 
>     >>> foo = 5.223701009526849e-05
>     >>> "{foo:5.0e}".format(foo=foo)
>     '5e-05'
> 

Or even better the "general" format, which also works for 0.9999:

    >>> "{foo:.1g}".format(foo=5.223701009526849e-5)
    '5e-05'
    >>> "{foo:.1g}".format(foo=0.9999)
    '1'

I guess all roads lead to Rome...




More information about the Python-list mailing list