Skip to content Skip to sidebar Skip to footer

Casting Float To String Without Scientific Notation

The float: fl = 0.000005 casts to String as str(fl)=='5e-06'. however, I want it to cast as str(fl)='0.000005' for exporting to CSV purposes. How do I achieve this?

Solution 1:

You can just use the standard string formatting option stating the precision you want

>>>fl = 0.000005>>>print'%.6f' % fl
0.000005

Solution 2:

Use

fl = 0.00005
s = '%8.5f' % fl
print s, type(s)

Gives

0.00005 <type'str'>

In case you want no extra digits, use %g (although it uses exponential notation for e.g. 0.000005). See for example:

fl = 0.0005
s = '%g' % fl
print s, type(s)

fl = 0.005
s = '%g' % fl
print s, type(s)

Gives

0.0005 <type'str'>
0.005 <type'str'>

Post a Comment for "Casting Float To String Without Scientific Notation"