Python 3 浮点小数点/精度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14540143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python 3 Float Decimal Points/Precision
提问by darksky
I am reading a text file with floating point numbers, all with either 1 or 2 decimal points. I am using float()to convert a line into a float, and raising a ValueErrorif that fails. I am storing all floats in a list. When printing it out, I'd like to print it out as a 2 decimal places floating point.
我正在阅读一个带有浮点数的文本文件,全部带有 1 或 2 个小数点。我正在使用float()将一条线转换为浮点数,ValueError如果失败则提高一个。我将所有浮点数存储在一个列表中。打印时,我想将其打印为 2 个小数位浮点数。
Assume I have a text file with the numbers -3,65, 9,17, 1. I read each one, and once I convert them to float and append them to a list. Now in Python 2, calling float(-3.65)returns -3.65. In Python 3 however, float(-3.65) returns-3.6499999999999999` which loses its precision.
假设我有一个包含数字 -3,65, 9,17, 1 的文本文件。我阅读了每个文件,然后将它们转换为浮点数并将它们附加到列表中。现在在 Python 2 中,调用float(-3.65)返回-3.65。然而,在 Python 3 中,float(-3.65) returns-3.6499999999999999` 失去了它的精度。
I want to print the list of floats, [-3.6499999999999999, 9.1699999999999999, 1.0]with 2 decimal points only. Doing something along the lines of '%.1f' % round(n, 1)would return a string. How can I return a list of all two decimal points of floats, and not strings? So far, I rounded it using [round(num, 2) for num in list]but would need to set the decimal points / precision instead of round().
我想打印浮点数列表,[-3.6499999999999999, 9.1699999999999999, 1.0]只有 2 个小数点。做一些类似的事情'%.1f' % round(n, 1)会返回一个字符串。如何返回浮点数的所有两个小数点的列表,而不是字符串?到目前为止,我使用四舍五入[round(num, 2) for num in list]但需要设置小数点/精度而不是round().
采纳答案by NPE
In a word, you can't.
一句话,你不能。
3.65cannot be represented exactly as a float. The number that you're getting is the nearest number to 3.65that has an exact floatrepresentation.
3.65不能完全表示为 a float。您得到的数字是最接近3.65具有精确float表示的数字。
The difference between (older?) Python 2 and 3 is purely due to the default formatting.
(旧的?)Python 2 和 3 之间的区别纯粹是由于默认格式。
I am seeing the following both in Python 2.7.3 and 3.3.0:
我在 Python 2.7.3 和 3.3.0 中看到以下内容:
In [1]: 3.65
Out[1]: 3.65
In [2]: '%.20f' % 3.65
Out[2]: '3.64999999999999991118'
For an exact decimal datatype, see decimal.Decimal.
有关精确的十进制数据类型,请参阅decimal.Decimal。
回答by slurry
The simple way to do this is by using the round buit-in.
做到这一点的简单方法是使用圆形内置。
round(2.6463636263,2)would be displayed as 2.65.
round(2.6463636263,2)将显示为2.65.
回答by Andrew E
The comments state the objective is to print to 2 decimal places.
评论指出目标是打印到小数点后两位。
There's a simple answer for Python 3:
Python 3 有一个简单的答案:
>>> num=3.65
>>> "The number is {:.2f}".format(num)
'The number is 3.65'
or equivalently with f-strings (Python 3.6+):
或等效于 f 字符串(Python 3.6+):
>>> num = 3.65
>>> f"The number is {num:.2f}"
'The number is 3.65'
As always, the float value is an approximation:
与往常一样,浮点值是一个近似值:
>>> "{}".format(num)
'3.65'
>>> "{:.10f}".format(num)
'3.6500000000'
>>> "{:.20f}".format(num)
'3.64999999999999991118'
I think most use cases will want to work with floats and then only printto a specific precision.
我认为大多数用例都希望使用浮点数,然后只打印到特定的精度。
Those that want the numbers themselvesto be stored to exactly 2 decimal digits of precision, I suggest use the decimaltype. More reading on floating point precisionfor those that are interested.
那些希望数字本身精确存储到 2 个十进制数字的精度,我建议使用小数类型。有兴趣的人可以阅读更多关于浮点精度的信息。
回答by ArifMustafa
Try to understand through this below function using python3
尝试使用 python3 通过下面的函数来理解
def floating_decimals(f_val, dec):
prc = "{:."+str(dec)+"f}" #first cast decimal as str
print(prc) #str format output is {:.3f}
return prc.format(f_val)
print(floating_decimals(50.54187236456456564, 3))
Output is : 50.542
输出为:50.542
Hope this helps you!
希望这对你有帮助!
回答by S.Ahmed
Try this:
尝试这个:
num = input("Please input your number: ")
num = float("%0.2f" % (num))
print(num)
I believe this is a lot simpler. For 1 decimal place use %0.1f. For 2 decimal places use %0.2fand so on.
我相信这要简单得多。对于 1 个小数位,使用%0.1f. 对于 2 个小数位使用%0.2f等。
Or, if you want to reduce it all to 2 lines:
或者,如果您想将其全部减少到 2 行:
num = float("%0.2f" % (float(input("Please input your number: "))))
print(num)

