I frequently enter values in scientific notation (e.g. 3.472e-07) for floats in Python. Is there a simple way to access the exponent of a given float in Python?
E.g. given x = 4.7820012347239297234e-7
, is there some way to just get the value after e (in this case, -7).
Thanks!
(1) Take the log of the number and round the value accordingly.
for val in [12345678901234567890.0, .0000004782]:
logval = math.log10(val)
print val, logval, round(logval - (0.5 if logval<0 else 0))
(2) Convert it to string, grab the first occurrence of "e" from the right end, and take the remainder of the string.
for val in [12345678901234567890.0, .0000004782]:
valstr = "{0:e}".format(val)
epos = valstr.rfind('e')
exponent = valstr[epos+1:]
print int(exponent)
Would either of those work for you?