带有浮点数的字符串格式中的 Python 精度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20586011/
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 precision in string formatting with float numbers
提问by daveoncode
I don't understand why, by formatting a string containing a float value, the precision of this last one is not respected. Ie:
我不明白为什么通过格式化包含浮点值的字符串,不尊重最后一个的精度。IE:
'%f' % 38.2551994324
returns:
返回:
'38.255199'
(4 signs lost!)
(4个标志丢失!)
At the moment I solved specifying:
目前我解决了指定:
'%.10f' % 38.2551994324
which returns '38.2551994324' as expected… but should I really force manually how many decimal numbers I want? Is there a way to simply tell to python to keep all of them?! (what should I do for example if I don't know how many decimals my number has?)
它按预期返回 '38.2551994324' ......但我真的应该手动强制我想要多少个十进制数吗?有没有办法简单地告诉python保留所有这些?!(例如,如果我不知道我的数字有多少个小数,我该怎么办?)
采纳答案by Martijn Pieters
but should I really force manually how many decimal numbers I want?Yes.
但我真的应该手动强制我想要多少个十进制数吗?是的。
And even with specifying 10 decimal digits, you are still not printing all of them. Floating point numbers don't have that kind of precision anyway, they are mostly approximationsof decimal numbers (they are really binary fractions added up). Try this:
即使指定了 10 个十进制数字,您仍然没有打印所有这些数字。浮点数无论如何都没有那种精度,它们大多是十进制数的近似值(它们实际上是二进制分数相加)。尝试这个:
>>> format(38.2551994324, '.32f')
'38.25519943239999776096738060005009'
there are many moredecimals there that you didn't even specify.
还有很多你甚至没有指定的小数。
When formatting a floating point number (be it with '%f' % number, '{:f}'.format(number)or format(number, 'f')), a defaultnumber of decimal places is displayed. This is no different from when using str()(or '%s' % number, '{}'.format(number)or format(number), which essentially use str()under the hood), only the number of decimals included by default differs; Python versions prior to 3.2 use 12 digits for the whole numberwhen using str().
格式化浮点数时(无论是'%f' % number,'{:f}'.format(number)还是format(number, 'f')),都会显示默认的小数位数。这与使用str()(or '%s' % number, '{}'.format(number)or format(number), 本质上是str()在幕后使用)时没有什么不同,只是默认包含的小数位数不同;Python版本之前3.2使用12位的整数使用时str()。
If you expect your rational number calculations to work with a specific, precisenumber of digits, then don't use floating point numbers. Use the decimal.Decimaltypeinstead:
如果您希望有理数计算使用特定的精确位数,则不要使用浮点数。改用decimal.Decimal类型:
Decimal “is based on a floating-point model which was designed with people in mind, and necessarily has a paramount guiding principle – computers must provide an arithmetic that works in the same way as the arithmetic that people learn at school.” – excerpt from the decimal arithmetic specification.
Decimal numbers can be represented exactly. In contrast, numbers like
1.1and2.2do not have exact representations in binary floating point. End users typically would not expect1.1 + 2.2to display as3.3000000000000003as it does with binary floating point.
十进制“基于浮点模型,该模型是为人而设计的,并且必然具有最重要的指导原则——计算机必须提供一种与人们在学校学习的算术相同的算术。” – 摘自十进制算术规范。
十进制数可以精确表示。相比之下,数字类似
1.1并且2.2在二进制浮点数中没有精确的表示。最终用户通常不希望像使用二进制浮点数那样1.1 + 2.2显示3.3000000000000003。
回答by K DawG
I would use the modern str.format()method:
我会使用现代str.format()方法:
>>> '{}'.format(38.2551994324)
'38.2551994324'
The modulo method for string formatting is now deprecated as per PEP-3101
根据PEP-3101,现在不推荐使用字符串格式化的模方法

