Meraj al Maksud
Meraj al Maksud

Reputation: 1582

How to print a decimal number without exponential form / scientific notation?

To print a floating point number without exponential form, we can write like this:

print('%f' % (10**-8))

But this generally displays zeroes due to precision error. Instead, we can use Decimal.decimal(float) to print a floating point number precisely. But that prints the number in exponential format i.e. 10e-10. And I cannot seem to combine them as I don't know how to format a Decimal like the way I used '%f' to format floats.

So my question is, how to get this thing done the way I want?

Note: The power -8 used here is just an example. The power could be variable. Also, I was wondering if I could print the differences of two numbers with high precision.

Upvotes: 4

Views: 4261

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195438

from decimal import Decimal
print('%.8f' % Decimal(10**-8))

Outputs:

0.00000001

To specify decimal places dynamically you can use this:

from decimal import Decimal

power = 10
print('%.*f' % (power, Decimal(10**-power)))

Prints:

0.0000000001

Upvotes: 3

Related Questions