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

后端 未结 3 1759
再見小時候
再見小時候 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:44

    Like this?

    >>> print('{:.100f}'.format(0.0000002345E-60))
    0.0000000000000000000000000000000000000000000000000000000000000000002344999999999999860343602938602754
    

    As you might notice from the output, it’s not really that clear how you want to do it. Due to the float representation you lose precision and can’t really represent the number precisely. As such it’s not really clear where you want the number to stop displaying.

    Also note that the exponential representation is often used to more explicitly show the number of significant digits the number has.

    You could also use decimal to not lose the precision due to binary float truncation:

    >>> from decimal import Decimal
    >>> d = Decimal('0.0000002345E-60')
    >>> p = abs(d.as_tuple().exponent)
    >>> print(('{:.%df}' % p).format(d))
    0.0000000000000000000000000000000000000000000000000000000000000000002345
    

提交回复
热议问题