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?
We can easily format to raw decimal places with "%f" which is same as "%. 6f" by default. "%f" % 8.99284722486562e-05 produces '0.000090' . "%f" % 0.01 produces '0.010000' .
Ceil. The ceil() function rounds off decimal to nearest upper integer.
It's python3? If so this should work: {res:.3E}
@edit It should work also with python2 - spec
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With