28

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?

jonrsharpe
  • 107,083
  • 22
  • 201
  • 376
mdpoleto
  • 569
  • 2
  • 5
  • 10
  • 2
    Have you considered searching separately for: 1. How to convert strings to numbers; and 2. How to convert numbers to strings? This isn't that difficult. – jonrsharpe Apr 24 '15 at 13:59

4 Answers4

27

I'm pretty sure you can do this with:

float("8.99284722486562e-02")
# and now with 'rounding'
"{:.8f}".format(float("8.99284722486562e-02"))
Jemshit Iskenderov
  • 8,415
  • 5
  • 60
  • 98
m0dem
  • 900
  • 1
  • 7
  • 14
13

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)

Puddle
  • 2,575
  • 1
  • 17
  • 32
3

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

jesterjunk
  • 2,184
  • 19
  • 17
2

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')
User
  • 56,228
  • 70
  • 174
  • 240
  • I was scouting this topic and saw this answer, do you have an explanation to why the first statement yeilds to False? – Ahmed Alhallag Sep 30 '20 at 18:40
  • @AhmedAlhallag See here: https://docs.python.org/3/tutorial/floatingpoint.html "In the same way, no matter how many base 2 digits you’re willing to use, the decimal value 0.1 cannot be represented exactly as a base 2 fraction. In base 2, 1/10 is the infinitely repeating fraction" – User Oct 01 '20 at 01:03