Select to view content in your preferred language

Scientific Notation to Double

587
2
01-26-2012 06:40 AM
WilliamIde
Emerging Contributor
Does anyone know of an elegant way to convert a string that has a number represented by scientific notation ( e.g.  .0340344E-4) to a double?  I and trying to normalize a set of shape files from different sources and one of my providers gave me a length field in sci notation.  All the others are doubles.  or strings as doubles.  (e.g. "23.5")  I can handle the second case.  Not sure how to do the first without resorting to "brute force" parsing.

Thanks
Tags (2)
0 Kudos
2 Replies
BruceNielsen
Frequent Contributor
You could use string substitution techniques:
 print '%13.11f' % float('.0340344E-4')
'0.00000340344'
0 Kudos
GerryGabrisch
Frequent Contributor
def SciNoteToFloat(x):
    '''Takes a string writen in scientific notation and
    returns a float'''
    x = x.lower()
    x = x.partition("e")
    return (float(x[0])) * (int("1" + "0" * (-1 * int(x[2]))))
mynum = "0.223E-7"
print SciNoteToFloat(mynum)
0 Kudos