python中需要更多的小数位
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14057835/
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
more decimal places needed in python
提问by user1551817
I am writing a python code, which needs to asses number to very many decimal places - around 14 or 15.
我正在编写一个 python 代码,它需要将数字评估到非常多的小数位 - 大约 14 或 15。
I expect results that increase smoothly, for example:
我希望结果平稳增加,例如:
0.123456789101997
0.123456789101998
0.123456789101999
0.123456789102000
0.123456789102001
But I seem to be only getting numbers (when I print them out) to 12 decimal places, so my numbers are actually jumping in steps:
但是我似乎只能将数字(当我打印出来时)精确到小数点后 12 位,所以我的数字实际上是在逐步跳跃:
0.123456789101
0.123456789101
0.123456789101
0.123456789102
0.123456789102
At first, I assumed that the number was listed to more decimal places behind the scenes, but just printed out to 12, but when I print the results, they seem to plot in steps also, rather than smoothly.
起初,我假设这个数字在幕后被列出到更多的小数位,但只是打印到 12,但是当我打印结果时,它们似乎也是逐步绘制的,而不是平滑的。
I'd be really grateful if anyone would point me in the right direction to fix this - thank you.
如果有人能指出我解决这个问题的正确方向,我将不胜感激 - 谢谢。
采纳答案by waitingkuo
How about trying the Decimal module?
试试 Decimal 模块怎么样?
In [2]: import decimal
In [3]: d = decimal.Decimal('0.123456789101997')
In [4]: print d
0.123456789101997
回答by Ashwini Chaudhary
Use repr(), printuses str()which reduces the number of decimal digits to 12 to make the output user friendly.
使用repr(),print用于str()将小数位数减少到 12 位,使输出更友好。
In [17]: a=0.123456789101997
In [18]: str(a)
Out[18]: '0.123456789102'
In [19]: repr(a)
Out[19]: '0.123456789101997'
or string formatting:
或字符串格式:
In [21]: "{0:.15f}".format(a)
Out[21]: '0.123456789101997'
回答by James Waldby - jwpat7
As suggested in previous answers, you can use Decimal numbers via the decimalmodule, or alternately can specify 15 decimal places when printing floating point values, to override the default of 12 places.
正如之前的答案所建议的,您可以通过小数模块使用小数,或者在打印浮点值时指定 15 位小数,以覆盖 12 位的默认值。
In many Python implementations, ordinary floating point numbers are IEEE 754-compatible (1, 2) “binary64” double precision numbers, so have effectively 53 bits in their mantissas. As 53*math.log(2)/math.log(10)is about 15.95, binary64 numbers support more than 15 decimal digits of precision but not quite 16.
在许多 Python 实现中,普通浮点数是 IEEE 754 兼容的 ( 1, 2) “binary64”双精度数,因此它们的尾数实际上有 53 位。由于53*math.log(2)/math.log(10)约15.95,binary64数字支持的精度大于15个的十进制数字,但并不完全16。
Here is an example you can try, shown with its output:
这是一个您可以尝试的示例,并显示其输出:
u=1e-15
v=0.123456789101997
for k in range(13):print '{:20.15f}'.format(v+k*u)
0.123456789101997
0.123456789101998
0.123456789101999
0.123456789102000
0.123456789102001
0.123456789102002
0.123456789102003
0.123456789102004
0.123456789102005
0.123456789102006
0.123456789102007
0.123456789102008
0.123456789102009
回答by James Waldby - jwpat7
You may choose to use the module decimalfor this purpose.
为此,您可以选择使用模块十进制。
You need to import the module.
您需要导入模块。
>>> from decimal import *
Then, you need to specify the number of decimal points you need by calling getcontext() function. The following code asks for 25 decimal points.
然后,您需要通过调用 getcontext() 函数来指定您需要的小数点数。以下代码要求 25 个小数点。
>>> getcontext().prec = 25
Then, you need to specify your arithmetic. The following code determines the value of pi up to 25 decimal points.
然后,您需要指定您的算术。以下代码确定最多 25 个小数点的 pi 值。
>>> Decimal(22) / Decimal(7)
The output is
输出是
>>> Decimal('3.142857142857142857142857')
回答by Pranab Sarkar
x=56.0
print("{:.2f}".format(x))
If we execute this code we will get-
如果我们执行此代码,我们将得到-
Output: 56.00
输出:56.00

