Search code examples
pythonpi

Python won't print more than 12 significant figures. How do I print more easily?


I was making a calculator and I wanted to use pi however python just shortens pi to 12 d.p. and I wanted the whole of pi.

I have a simple code in a file called getpi.py:

def getpisend():
   pi=*pi to a million places*
return pi

and I receive it with this code:

import getpi
print getpi.getpisend()

However it only prints this:

3.14159265359

How do I make it print all one million digits without making it a string. Because then I can't use it in a calculator. I have tried changing it into a string and then back to an float but get this error:

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

` EDIT: It is probably stupid to do this because it isn't actually usable because you would have to write/print loads of data but this has helped me understand python much more 😀! Special thanks to @qxz for showing the theory!


Solution

  • use decimal.Decimal:

    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'