How scientific notation inclusion is decided implicitly in python?

Viewed 34

What are the criterias in python for scientific notation inclusion in a calculation result?

d1 = Decimal('500')
d2 = Decimal('0.25')
print d1/d2

This gives the result 2.0E+3

If i include 2 zeroes after decimal point for 500, that is ,

d1 = Decimal('500.00')
d2 = Decimal('0.25')
print d1/d2

This gives the result 2000

Also how can it be made sure that there are no scientific notation in the result?

1 Answers

See FAQ in the official docs:

Q. Some decimal values always print with exponential notation. Is there a way to get a non-exponential representation?

A. For some values, exponential notation is the only way to express the number of significant places in the coefficient. For example, expressing 5.0E+3 as 5000 keeps the value constant but cannot show the original’s two-place significance.

If an application does not care about tracking significance, it is easy to remove the exponent and trailing zeroes, losing significance, but keeping the value unchanged:

def remove_exponent(d):
    return d.quantize(Decimal(1)) if d == d.to_integral() else d.normalize()
>>> remove_exponent(Decimal('5E+3'))
Decimal('5000')
Related