Python根据用户指定的数字舍入PI值

Python根据用户指定的数字舍入PI值,python,Python,我的代码: import math def CalPI(precision): answer = round((math.pi),precision) return answer precision=raw_input('Enter number of digits you want after decimal:') try: roundTo=int(precision) print CalPI(roundTo) except: print 'Err

我的代码:

import math
def CalPI(precision):
    answer = round((math.pi),precision)
    return answer

precision=raw_input('Enter number of digits you want after decimal:')

try:
    roundTo=int(precision)
    print CalPI(roundTo)

except:
    print 'Error'
当我运行这段代码时,我得到的最大输出只有11位小数。 但是,我希望根据用户提供的输入生成输出

谁能告诉我哪里出了问题


提前谢谢你

如果在打印中使用
repr
,您将有15位数字:3.141592653589793

如果需要更多数字(直到50),请使用

(感谢Stefan的精确性:)但正如他再次指出的:不要相信数字15之后的数字,所以1000位的程序是最好的)

对于更多的数字,您可以自己计算pi,如下所示:


如果在打印中使用
repr
,您将有15位数字:3.141592653589793

如果需要更多数字(直到50),请使用

(感谢Stefan的精确性:)但正如他再次指出的:不要相信数字15之后的数字,所以1000位的程序是最好的)

对于更多的数字,您可以自己计算pi,如下所示:


实际上,math.pi包含3.141592653589793115997963468544185161590576171875。刚刚看到您的编辑。也许我应该指出,虽然math.pi确实包含该值,但前15个左右的数字并不是来自pi。它们只是浮点表示法的产物。感谢您的建议Jean François Fabreacly,math.pi包含3.14159265358979311599796346854418516190576171875。刚刚看到您的编辑。也许我应该指出,虽然math.pi确实包含该值,但前15个左右的数字并不是来自pi。它们只是浮点表示法的产物。感谢您对StackOverflow的建议Jean-François FabreWelcome!请编辑你的帖子。首先,包含一些文本以指示您试图显示的内容(看起来您只是在发布代码,这是不鼓励的。)其次,将代码粘贴到中,选择它,然后单击
{}
小部件以使其作为代码呈现。这会将其右移四个空格,使其呈现为代码。避免使用制表符,除非它们设置为4个空格。欢迎使用StackOverflow!请编辑你的帖子。首先,包含一些文本以指示您试图显示的内容(看起来您只是在发布代码,这是不鼓励的。)其次,将代码粘贴到中,选择它,然后单击
{}
小部件以使其作为代码呈现。这会将其右移四个空格,使其呈现为代码。避免使用制表符,除非它们设置为4个空格。
nb_digits = 40
print(format(math.pi, '.%dg' % nb_digits))
"""
Math provides fast square rooting
Decimal gives the Decimal data type which is much better than Float
sys is needed to set the depth for recursion.
"""
from __future__ import print_function
import math, sys
from decimal import *
getcontext().rounding = ROUND_FLOOR
sys.setrecursionlimit(100000)

python2 = sys.version_info[0] == 2
if python2:
    input = raw_input()

def factorial(n):
    """
    Return the Factorial of a number using recursion
    Parameters:
    n -- Number to get factorial of
    """
    if not n:
        return 1
    return n*factorial(n-1)


def getIteratedValue(k):
    """
    Return the Iterations as given in the Chudnovsky Algorithm.
    k iterations give k-1 decimal places. Since we need k decimal places
    make iterations equal to k+1

    Parameters:
    k  -- Number of Decimal Digits to get
    """
    k = k+1
    getcontext().prec = k
    sum=0
    for k in range(k):
        first = factorial(6*k)*(13591409+545140134*k)
        down = factorial(3*k)*(factorial(k))**3*(640320**(3*k))
        sum += first/down 
    return Decimal(sum) 

def getValueOfPi(k):
    """
    Returns the calculated value of Pi using the iterated value of the loop
    and some division as given in the Chudnovsky Algorithm
    Parameters:
    k -- Number of Decimal Digits upto which the value of Pi should be calculated
    """
    iter = getIteratedValue(k)
    up = 426880*math.sqrt(10005)
    pi = Decimal(up)/iter 

    return pi

def shell():
    """
    Console Function to create the interactive Shell.
    Runs only when __name__ == __main__ that is when the script is being called directly
    No return value and Parameters
    """
    print ("Welcome to Pi Calculator. In the shell below Enter the number of digits upto which the value of Pi should be calculated or enter quit to exit")

    while True:
        print (">>> ", end='')
        entry = input()
        if entry == "quit":
            break
        if not entry.isdigit():
            print ("You did not enter a number. Try again")
        else:
            print (getValueOfPi(int(entry)))

if __name__=='__main__':
    shell()