在没有科学记数法的情况下将浮子铸成字符串

2024-09-30 01:21:01 发布

您现在位置:Python中文网/ 问答频道 /正文

浮动汇率:

fl = 0.000005

String强制转换为str(fl)=='5e-06'。但是,我希望它转换为str(fl)='0.000005',以便导出到CSV目的

我如何做到这一点


Tags: csv目的stringstrfl浮动汇率
2条回答

使用

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

给予

0.00005 <type 'str'>

如果不需要额外的数字,请使用%g(尽管它使用指数表示法,例如0.000005)。例如,见:

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

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

给予

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

您只需使用标准字符串格式选项说明所需的精度

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

相关问题 更多 >

    热门问题