I have to convert exponential strings, like 6.5235375356299998e-07
,
to a float value, and display the result of my computation like 0.00000065235...
How can I do this in a Python program?
Asked
Active
Viewed 1.0k times
4

Gilles 'SO- stop being evil'
- 104,111
- 38
- 209
- 254

StefanS
- 261
- 3
- 4
- 10
-
4Did you try `float("6.5235375356299998e-07")`? – Sven Marnach Feb 08 '12 at 13:02
-
>>> a = 6.52353753563E-7 >>> float(a) 6.5235375356299998e-07 – StefanS Feb 08 '12 at 13:03
-
yes, i tried it, but the result is a exponent, too – StefanS Feb 08 '12 at 13:04
-
2@StefanS: What do you mean by "a float value" then? – kennytm Feb 08 '12 at 13:05
-
I think he wants it *displayed* as `0.00000065235375356299998`. – Tim Pietzcker Feb 08 '12 at 13:07
-
See http://en.wikipedia.org/wiki/E_notation for an explanation. – Sven Marnach Feb 08 '12 at 13:10
1 Answers
11
6.5235375356299998e-07
is a perfectly legal float even if there is an e
in it. You can do the whole calculation with it:
>>> 6.5235375356299998e-07 * 10000000
6.5235375356300001
>>> 6.5235375356299998e-07 + 10000000
10000000.000000652
In the second case, many digits will disappear because of the precision of a python's float.
If you need the string representation without e
, try this:
>>> '{0:.20f}'.format(6.5235375356299998e-07)
'0.00000065235375356300'
but it will become a string and you won't be able to do any calculus with it any more.

eumiro
- 207,213
- 34
- 299
- 261