我正在制作一个计算器而我想使用pi但是python只是将pi缩短到12 d.p.我想要整个pi。
我在名为getpi.py
的文件中有一个简单的代码:
def getpisend():
pi=*pi to a million places*
return pi
我收到了这段代码:
import getpi
print getpi.getpisend()
然而它只打印这个:
3.14159265359
如何使其打印所有一百万个数字而不使其成为字符串。因为那时我无法在计算器中使用它。 我已经尝试将其更改为字符串,然后返回到浮点数但是出现此错误:
Traceback (most recent call last):
File "use-pi.py", line 2, in <module>
print (int(pifunc.getpisend()))
ValueError: invalid literal for int() with base 10:3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381
` 编辑:执行此操作可能很愚蠢,因为它实际上并不可用,因为您必须编写/打印大量数据,但这有助于我更好地理解python!特别感谢@qxz展示理论!
答案 0 :(得分:6)
In [57]: f = decimal.Decimal('3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359
...: 408128481117450284102701938521105559644622948954930381')
In [58]: f
Out[58]: Decimal('3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381')
In [59]: f.to_eng_string()
Out[59]: '3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381'