Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert scientific notation to decimals

I have numbers in a file (so, as strings) in scientific notation, like:

8.99284722486562e-02 

but I want to convert them to:

0.08992847

Is there any built-in function or any other way to do it?

like image 630
mdpoleto Avatar asked Apr 24 '15 13:04

mdpoleto


People also ask

How do you reverse scientific notation?

Express each number in scientific notation. To change scientific notation to standard notation, we reverse the process, moving the decimal point to the right. Add zeros to the end of the number being converted, if necessary, to produce a number of the proper magnitude. Lastly, we drop 10 and its power.


4 Answers

I'm pretty sure you can do this with:

float("8.99284722486562e-02") # and now with 'rounding' "{:.8f}".format(float("8.99284722486562e-02")) 
like image 196
m0dem Avatar answered Sep 30 '22 11:09

m0dem


I'm making this answer since the top voted one has misinformation and so i can explain my improvements.

TL;DR: Use ("%.17f" % n).rstrip('0').rstrip('.')


By default Python formats to scientific notation if there's 5 or more zeroes at the beginning.
0.00001 / 1e-05 formats to "1e-05".
0.0001 / 1e-04 formats to "0.0001".

So of course 8.99284722486562e-02 will format to "0.0899284722486562" already.
A better example would've been 8.99284722486562e-05. (0.00008992847224866)

We can easily format to raw decimal places with "%f" which is same as "%.6f" by default.
"%f" % 8.99284722486562e-05 produces '0.000090'.
"%f" % 0.01 produces '0.010000'.


By default floats display upto 17 decimal places.
0.1234567898765432123 - (19 dp input)
0.12345678987654321 - (17 dp output)

So if we did "%.17f" % 8.99284722486562e-02 we'd get '0.08992847224865620'. (note the extra 0)
But if we did "%.17f" % 0.0001 we surely wouldn't want '0.00010000000000000'.

So to remove the trailing zeroes we can do: ("%.17f" % n).rstrip('0').rstrip('.')
(Notice we also strip the decimal point incase the number has no fraction left)


Also there's counterparts to %f:
%f shows standard notation
%e shows scientific notation
%g shows default (scientific if 5 or more zeroes)

like image 33
Puddle Avatar answered Sep 30 '22 09:09

Puddle


The scientific notation can be converted to a floating point number with float.

   In [1]:  float("8.99284722486562e-02")
Out [1]:   0.0899284722486562

The float can be rounded with format and then float can be used on the string to return the final rounded float.

   In [2]:  float("{:.8f}".format(float("8.99284722486562e-02")))
Out [2]:   0.08992847




2022 edit for No Sound's comment:

I learned this solution from here (archived)

The following solutions work for larger numbers.

Solution 1)

import numpy as np

print(np.format_float_positional(1.32456e-12, trim='-'))
print(np.format_float_positional(1.32456e-24, trim='-'))
print(np.format_float_positional(1.32456e12, trim='-'))
print(np.format_float_positional(1.32456e24, trim='-'))

# Output: 0.00000000000132456
#         0.00000000000000000000000132456
#         1324560000000
#         1324560000000000000000000

Solution 2)

same as above accept this time using a lambda function

import numpy as np

pretty_print = lambda x: np.format_float_positional(x, trim="-")

print(pretty_print(1.32456e-12))
print(pretty_print(1.32456e-24))
print(pretty_print(1.32456e12))
print(pretty_print(1.32456e24))

# Output: 0.00000000000132456
#         0.00000000000000000000000132456
#         1324560000000
#         1324560000000000000000000
like image 45
jesterjunk Avatar answered Sep 30 '22 10:09

jesterjunk


As you may know floating point numbers have precision problems. For example, evaluate:

>>> (0.1 + 0.1 + 0.1) == 0.3
False

Instead you may want to use the Decimal class. At the 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')
like image 40
User Avatar answered Sep 30 '22 10:09

User