问题
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?
回答1:
Use
fl = 0.00005
s = str('%8.5f' % fl)
print s, type(s)
Gives
0.00005 <type 'str'>
In case you want no extra digits, use %g
fl = 0.0005
s = str('%g' % fl)
print s, type(s)
fl = 0.005
s = str('%g' % fl)
print s, type(s)
Gives
0.0005 <type 'str'>
0.005 <type 'str'>
回答2:
You can just use the standard string formatting option stating the precision you want
>>> fl = 0.000005
>>> print '%.6f' % fl
0.000005
来源:https://stackoverflow.com/questions/25665523/casting-float-to-string-without-scientific-notation