Converting Scientific Notation to Decimals
I have numbers in a file (like strings) in scientific notation, for example:
8.99284722486562e-02
but I want to convert them to:
0.08992847
Is there a built in function or any other way to do this?
I'm sure you can do it with
float("8.99284722486562e-02")
# and now with 'rounding'
"{:.8f}".format(float("8.99284722486562e-02"))
Scientific notation can be converted to floating point number float
.
In [1]: float("8.99284722486562e-02")
Out [1]: 0.0899284722486562
float
can be rounded with format
and then float
can be used in a string to return the final rounded float.
In [2]: float("{:.8f}".format(float("8.99284722486562e-02")))
Out [2]: 0.08992847
As you know, floating point numbers have precision issues. For example, evaluate:
>>> (0.1 + 0.1 + 0.1) == 0.3
False
You can use the Decimal class instead . In python interpreter:
>>> import decimal
>>> tmp = decimal.Decimal('8.99284722486562e-02')
Decimal('0.0899284722486562')
>>> decimal.getcontext().prec = 7
>>> decimal.getcontext().create_decimal(tmp)
Decimal('0.08992847')