Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 235
Dartmouth Avatar asked Sep 18 '16 16:09

Dartmouth


People also ask

How do you change a decimal to scientific notation in Python?

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' .

How do I get rid of decimal in Numpy?

Ceil. The ceil() function rounds off decimal to nearest upper integer.


1 Answers

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

@edit It should work also with python2 - spec

like image 108
Mateusz Soltysik Avatar answered Nov 04 '22 14:11

Mateusz Soltysik