[英]Add zeros to a float after the decimal point in Python
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?我将如何将浮点数写入文件,例如
my_float = 2.0
,如my_float = 2.00000
?
Format it to 6 decimal places:将其格式化为 6 位小数:
format(value, '.6f')
Demo:演示:
>>> format(2.0, '.6f')
'2.000000'
The format()
function turns values to strings following the formatting instructions given.format()
函数按照给定的格式说明将值转换为字符串。
I've tried n ways but nothing worked that way I was wanting in, at last, this worked for me.我已经尝试了 n 种方法,但没有任何方法能达到我想要的效果,最后,这对我有用。
foo = 56
print (format(foo, '.1f'))
print (format(foo, '.2f'))
print (format(foo, '.3f'))
print (format(foo, '.5f'))
output:
56.0
56.00
56.000
56.00000
Meaning that the 2nd argument of format
takes the decimal places you'd have to go up to.这意味着
format
的第二个参数采用您必须达到的小数位。 Keep in mind that format
returns string.请记住,
format
返回字符串。
An answer using the format() command is above, but you may want to look into the Decimal standard library object if you're working with floats that need to represent an exact value.使用 format() 命令的答案在上面,但如果您正在使用需要表示精确值的浮点数,您可能需要查看 Decimal 标准库对象。 You can set the precision and rounding in its context class, but by default it will retain the number of zeros you place into it:
您可以在其上下文类中设置精度和舍入,但默认情况下它将保留您放入其中的零数:
>>> import decimal
>>> x = decimal.Decimal('2.0000')
>>> x
Decimal('2.0000')
>>> print x
2.0000
>>> print "{0} is a great number.".format(x)
2.0000 is a great number.
I've had problems with using variables in f strings.我在 f 字符串中使用变量时遇到了问题。 When all else fails, read the manual :)
当所有其他方法都失败时,请阅读手册:)
"A consequence of sharing the same syntax as regular string literals is that characters in the replacement fields must not conflict with the quoting used in the outer formatted string literal." “与常规字符串文字共享相同语法的结果是替换字段中的字符不得与外部格式化字符串文字中使用的引用冲突。”
https://docs.python.org/3/reference/lexical_analysis.html#f-strings https://docs.python.org/3/reference/lexical_analysis.html#f-strings
Case in point:案例:
my_number = 90000
zeros = '.2f'
my_string = f"{my_number:,{zeros}}"
print (my_string)
90,000.00
my_string = f'{my_number:,{zeros}}' my_string = f'{my_number:,{zeros}}'
will not work, because of the single quotes.不会工作,因为单引号。
Quotes containing the f string and the string variable used in the f string should be different .包含 f 字符串的引号和 f 字符串中使用的字符串变量应该不同。
If using single quotes for the string variable, use double quotes for the f module and vice versa.如果对字符串变量使用单引号,则对 f 模块使用双引号,反之亦然。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.