chirag shah - 5 months ago 13

Python Question

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:')

try:

roundTo=int(precision)

print CalPI(roundTo)

except:

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.

Can anyone tell me where I am going wrong?

Thank you in advance!

Answer

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: