0

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 Answers2

3

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.

Nick ODell
  • 15,465
  • 3
  • 32
  • 66
0

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)
surge10
  • 622
  • 6
  • 18