How can I print many significant figures in Python?

后端 未结 6 1962
独厮守ぢ
独厮守ぢ 2020-12-16 12:53

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 c

6条回答
  •  伪装坚强ぢ
    2020-12-16 13:08

    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.

提交回复
热议问题