Search code examples
pythonpython-3.xformattingformatscientific-notation

Scientific notation not working in python


I am using python version 3.9.5, and trying to use scientific notation using the format method, but it's not working!

I have searched across the web for this, but didn't get anything.

I used the formatting method of format(num, f'.{precision}e') to turn it into scientific notation, and it works for non-decimal numbers, but when I use decimal numbers, it doesn't work:

num = int(10**9) # 1000000000
dec = int(10**-4) # 0.0001

print(format(num, '.5e'))
print(format(dec, '.5e'))

# 1.00000e+09
# 0.00000e+00

As you can see, the second output is meant to be a different result but I got 0.00000e+00


Can somebody please help me solve this, thanks in advance.


Solution

  • int(x) will always be an integer.

    print(int(10**-4))
    
    #output 
    0
    

    you need to do:

    print(format(float(10**-4), '.5e'))
    
    #output
    1.00000e-04