How to print floating point numbers as it is without any truncation in python?

后端 未结 3 1761
再見小時候
再見小時候 2020-12-18 06:28

I have some number 0.0000002345E^-60. I want to print the floating point value as it is. What is the way to do it? print %f truncates it to 6 digits. Also %n.nf gives fixed

3条回答
  •  情歌与酒
    2020-12-18 06:56

    You can use decimal.Decimal:

    >>> from decimal import Decimal
    >>> str(Decimal(0.0000002345e-60))
    '2.344999999999999860343602938602754401109865640550232148836753621775217856801120686600683401464097113374472942165409862789978024748827516129306833728589548440037314681709534891496105046826414763927459716796875E-67'
    

    This is the actual value of float created by literal 0.0000002345e-60. Its value is a number representable as python float which is closest to actual 0.0000002345 * 10**-60.

    float should be generally used for approximate calculations. If you want accurate results you should use something else, like mentioned Decimal.

提交回复
热议问题