How can I convert a number in scientific notation to a tuple that contains the significand and the exponent? For example:
4e+3
→ (4, 3)
4e-3
→ (4, -3)
How can I convert a number in scientific notation to a tuple that contains the significand and the exponent? For example:
4e+3
→ (4, 3)
4e-3
→ (4, -3)
you could use
def scientific_to_tuple (float_number)
# Assume that float_number = 0.0034
scientific_string = f"{float_number:e}"
# after that, scientific_string looks like '3.400000e-03'
return tuple([float(item) for item in scentific_string.split("e")])
edit Note: I was tired so the method returned string. I fixed it. Actually, if tuple is not required, you can omit tuple casting. like
return [float(item) for item in scentific_string.split("e")]
Maybe you want this modified version of Ahmet Dundar's answer:
def scientific_to_tuple (float_number):
scientific_string = f"{float_number:e}"
a, b = scientific_string.split("e")
f = float(a)
i = int(f)
a = i if f == i else f
return (a, int(b))