Search code examples

How can I print many significant figures in Python?

For a scientific application I need to output very precise numbers, so I have to print 15 significant figures. There are already questions on this topic here, but they all concern with truncating the digits, not printing more.

I realized that the print function converts the input float to a 10 character string. Also, I became aware of the decimal module, but that does not suit my needs.

So the question is, how can I easily print a variable amount of signifcant figures of my floats, where I need to display more than 10?


  • You could use the string formatting operator %:

    In [3]: val = 1./3
    In [4]: print('%.15f' % val)

    or str.format():

    In [8]: print(str.format('{0:.15f}', val))
    Out[8]: '0.333333333333333'

    In new code, the latter is the preferred style, although the former is still widely used.

    For more info, see the documentation.