user2793286
user2793286

Reputation: 177

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

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 numbers. What is the way to print without truncation.

Upvotes: 6

Views: 4698

Answers (3)

poke
poke

Reputation: 387507

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

Upvotes: 3

selllikesybok
selllikesybok

Reputation: 1225

If I understand, you want to print a float?

The problem is, you cannot print a float.

You can only print a string representation of a float. So, in short, you cannot print a float, that is your answer.

If you accept that you need to print a string representation of a float, and your question is how specify your preferred format for the string representations of your floats, then judging by the comments you have been very unclear in your question.

If you would like to print the string representations of your floats in exponent notation, then the format specification language allows this:

{:g} or {:G}, depending whether or not you want the E in the output to be capitalized). This gets around the default precision for e and E types, which leads to unwanted trailing 0s in the part before the exponent symbol.

Assuming your value is my_float, "{:G}".format(my_float) would print the output the way that the Python interpreter prints it. You could probably just print the number without any formatting and get the same exact result.

If your goal is to print the string representation of the float with its current precision, in non-exponentiated form, User poke describes a good way to do this by casting the float to a Decimal object.

If, for some reason, you do not want to do this, you can do something like is mentioned in this answer. However, you should set 'max_digits' to sys.float_info.max_10_exp, instead of 14 used in the answer. This requires you to import sys at some point prior in the code.

A full example of this would be:

import math
import sys

def precision_and_scale(x):
    max_digits = sys.float_info.max_10_exp
    int_part = int(abs(x))
    magnitude = 1 if int_part == 0 else int(math.log10(int_part)) + 1
    if magnitude >= max_digits:
        return (magnitude, 0)
    frac_part = abs(x) - int_part
    multiplier = 10 ** (max_digits - magnitude)
    frac_digits = multiplier + int(multiplier * frac_part + 0.5)
    while frac_digits % 10 == 0:
        frac_digits /= 10
    scale = int(math.log10(frac_digits))
    return (magnitude + scale, scale)

f = 0.0000002345E^-60

p, s = precision_and_scale(f)

print "{:.{p}f}".format(f, p=p)

But I think the method involving casting to Decimal is probably better, overall.

Upvotes: 0

zch
zch

Reputation: 15278

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.

Upvotes: 0

Related Questions