Converting Scientific Notation to Decimals
3 answers
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
+1
source to share
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')
0
source to share