Dartmouth Dartmouth - 2 months ago 14
Python Question

Python numpy scientific notation limit decimals

I'm trying to reduce the number of decimals that I'm getting after some calculations. The

print()
where my problem arises looks like this:

print("Mean resistivity: {res} Ohm m".format(res=np.mean(resistivity)))


And it outputs this:

Mean resistivity: 1.6628449915450776e-08 Ohm m


Now I want to reduce the number of decimal places that are printed to 3. I tried doing it with string formatting, like this:

print("Mean resistivity: {res:.3f} Ohm m".format(res=np.mean(resistivity)))


However, this code prints:

Mean resistivity: 0.000 Ohm m


What I actually want is this:

Mean resistivity: 1.663e-8 Ohm m


How can I format
res
to only be displayed as scientific notation but with only 3 decimal places?

Answer

It's python3? Is so this should works: {res:.3E}

@edit It should work also with python2 - spec

Comments