6
votes

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?

1
Use a format print specifier - dawg

1 Answers

6
votes

It's python3? If so this should work: {res:.3E}

@edit It should work also with python2 - spec