[Tutor] Python 3.1: How to print a very large number to n significant digits?

Richard D. Moores rdmoores at gmail.com
Mon Nov 23 19:28:26 CET 2009


Can't find the answer in the docs for 3.1

To print 123**34.6 to 5 sig digits,

print("%.*e" % (4, 123**34.6))

will do the job:

>>> print("%.*e" % (4, 123**34.6))
2.0451e+72

However, if the number is 123**346, using

print("%.*e" % (4, 123**346))

gets me

>>> print("%.*e" % (5, 123**346))
Traceback (most recent call last):
  File "<pyshell#28>", line 1, in <module>
    print("%.*e" % (5, 123**346))
OverflowError: Python int too large to convert to C double

So how to do it?

Thanks,

Dick Moores


More information about the Tutor mailing list