I am creating a length calculator and need to format it so it doesn't show 1e-5
from going to mm
to km
. i have tried '{:.6}'.format()
but doesn't seem to work as still outputs it as 1e-5.
Any help on what to do to get rid of this?
I am creating a length calculator and need to format it so it doesn't show 1e-5
from going to mm
to km
. i have tried '{:.6}'.format()
but doesn't seem to work as still outputs it as 1e-5.
Any help on what to do to get rid of this?
Use the f
presentation type insteaf of the default (g
with a small modification):
'{:.6f}'.format(floating_point_number)
See the Format Specification Mini-Language documentation:
'f'
Fixed point. Displays the number as a fixed-point number. The default precision is6
.[...]
'g'
General format. For a given precision p >= 1, this rounds the number to p significant digits and then formats the result in either fixed-point format or in scientific notation, depending on its magnitude.[...]
None
Similar to'g'
, except that fixed-point notation, when used, has at least one digit past the decimal point. The default precision is as high as needed to represent the particular value. The overall effect is to match the output ofstr()
as altered by the other format modifiers.
Note that if all you are doing is formatting a float (and not include any other string in your str.format()
template), you may as well avoid having to parse the template and use the format()
function directly:
format(floating_point_number, '.6f')
Demo:
>>> fp = 1e-5
>>> fp
1e-05
>>> format(fp, '.6f')
'0.000010'
>>> '{:.6f}'.format(fp)
'0.000010'