簡體   English   中英

在沒有科學記數法的情況下將浮點數轉換為字符串

[英]Casting float to string without scientific notation

浮動:

fl = 0.000005

轉換為String作為str(fl)=='5e-06' 但是,我希望它轉換為str(fl)='0.000005'以導出到CSV目的。

我如何實現這一目標?

您可以只使用標准字符串格式選項說明您想要的精度

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

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'>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM