Ksh*_*ogi 4 python printing variables pi
我是Python的初学者,我对PI有疑问.
>>> import math
>>> p = math.pi
>>> print p
3.14159265359
>>> math.pi
3.141592653589793
Run Code Online (Sandbox Code Playgroud)
Tim*_*Tim 13
为什么两者的位数不同?
一个是"计算" __str__
,另一个是__repr__
:
>>> print repr(math.pi)
3.141592653589793
>>> print str(math.pi)
3.14159265359
Run Code Online (Sandbox Code Playgroud)
print
使用__str__
对象的返回值来确定要打印的内容.只是做math.pi
用__repr__
.
如何在不使用Chudnovsky算法的情况下将Pi的值增加到更多小数位?
您可以显示更多的数字有format()
像这样
>>> print "pi is {:.20f}".format(math.pi)
pi is 3.14159265358979311600
Run Code Online (Sandbox Code Playgroud)
其中20是小数位数.更多信息在文档中