How to return scientific notation with only a few decimal places with the notation still intact?

297 Views Asked by At

How to return scientific notation to decimal places with scientific notation still in python?

So I have a value like: 1.197474567059189e-09

How do I return something like 1.1974e-09 instead?

Thanks!

If I use round to say, 5 decimal places, it'll just return 0.00000. I could round to many many decimal places, but the value I'm using could be e-08 or e-10 at some point so I want it to keep working if that's the case.

2

There are 2 best solutions below

0
Nick ODell On

You can use the e format specifier, which uses scientific notation, with a specified number of significant digits.

Example:

value =  1.197474567059189e-09
print(f'{value:.4e}')

Output:

1.1975e-09

The 4 represents the number of digits after the decimal place to print out.

0
surge10 On

You should specify the type e instead. So for your 5 decimal places you should do

a = 1.197474567059189e-09

b = f"{a:.5e}"

print(b)