Casting float to string without scientific notation

99封情书 提交于 2019-12-12 16:23:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!