chirag shah chirag shah - 3 months ago 6
Python Question

Python-Rounding the value of PI according to the number specified by user

My Code :

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

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

print CalPI(roundTo)

print 'Error'

When I run this code I get the output max only upto 11 decimal places.
However I want to generate the output according to the input given by user.

This is my output snip:

Can anyone tell me where I am going wrong?

Thank you in advance!


If you use repr in your print you'll have 15 digits: 3.141592653589793.

If you want more digits (until 50) use

nb_digits = 40
print(format(math.pi, '.%dg' % nb_digits))

(thanks Stefan for the precision :) but as he stated again: don't trust digits after digit 15 so the 1000 digit program is the best).

For even more digits, compute pi yourself just like here:

1000 digits of pi in python