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

Wayne Werner waynejwerner at gmail.com
Mon Nov 23 21:23:54 CET 2009


On Mon, Nov 23, 2009 at 12:28 PM, Richard D. Moores <rdmoores at gmail.com>wrote:

> 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
>

Try the decimal module:

http://docs.python.org/library/decimal.html

HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091123/526f37bd/attachment.htm>


More information about the Tutor mailing list