問題描述
我正在從文件中讀取數據,對其進行修改并將其寫入另一個文件.新文件將被另一個程序讀取,因此保留準確的格式至關重要
I am reading in data from a file, modify it and write it to another file. The new file will be read by another program and therefore it is crucial to carry over the exact formatting
例如,我的輸入文件中的一個數字是:
for example, one of the numbers on my input file is:
1.000000
我的腳本對列應用了一些數學運算并應該返回
my script applies some math to the columns and should return
2.000000
但是當前返回的是
2.0
如何將浮點數(例如 my_float = 2.0
,作為 my_float = 2.00000
)寫入文件?
How would I write a float for example my_float = 2.0
, as my_float = 2.00000
to a file?
推薦答案
格式化到小數點后6位:
Format it to 6 decimal places:
format(value, '.6f')
演示:
>>> format(2.0, '.6f')
'2.000000'
format()
函數按照給定的格式化說明將值轉換為字符串.
The format()
function turns values to strings following the formatting instructions given.
這篇關于Python中小數點后的浮點數加零的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!