Home > Software engineering >  Convert a number in scientific notation to a tuple with the significand and the exponent
Convert a number in scientific notation to a tuple with the significand and the exponent

Time:09-28

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)

CodePudding user response:

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(scientific_string.split("e"))

CodePudding user response:

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))
  • Related