How can I print many significant figures in Python?
You could use the string formatting operator %
:
In [3]: val = 1./3
In [4]: print('%.15f' % val)
0.333333333333333
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.
Let:
>>> num = 0.0012345
For 3 significant figures:
>>> f'{num:.3}'
'0.00123'
For 3 decimal places:
>>> f'{num:.3f}'
'0.001'
See the "presentation types for floating point and decimal" table at the bottom of this section for any additional requirements provided by e, E, f, F, g, G, n, %, None
.