编程语言
首页 > 编程语言> > python-将浮点数转换为字符串而无需科学计数法

python-将浮点数转换为字符串而无需科学计数法

作者:互联网

浮点数:

fl = 0.000005

将字符串转换为str(fl)==’5e-06′.但是,我希望将其转换为str(fl)=’0.000005’以便导出到CSV.

我该如何实现?

解决方法:

采用

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

0.00005 <type 'str'>

如果您不需要多余的数字,请使用%g

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

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

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

标签:scientific-notation,casting,double,string,python
来源: https://codeday.me/bug/20191121/2050773.html