在Python中将小数点后的零添加到浮点数 [英] Add zeros to a float after the decimal point in Python
问题描述
1.000000
我的脚本对列应用了一些数学运算,并且应该返回
2.000000
但是目前返回的是
$ $ $ $ $ $ $ $ $ $ $ $ b
我如何写一个float,例如 my_float = 2.0
,作为 my_float = 2.00000
到一个文件? 格式它到小数点后6位:
$ $ p $ code格式(value,'.6f')
演示:
>>>格式(2.0,'.6f')
'2.000000'
format()
函数将值转换为格式之后的字符串给出指示。
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
But what is currently returned is
2.0
How would I write a float, for example my_float = 2.0
, as my_float = 2.00000
to a file?
Format it to 6 decimal places:
format(value, '.6f')
Demo:
>>> format(2.0, '.6f')
'2.000000'
The format()
function turns values to strings following the formatting instructions given.
这篇关于在Python中将小数点后的零添加到浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!