Or we can specify like this for three decimal places.
print(np.round(ar,decimals=3))
Output
[1.571 2.094 2.792 3.142]
out
We can store the output in an array. We used shape() to create array (ar_out) of same shape() of our main array ar.
We used ones() to create array of same shape() of our input array with degree values.
import numpy as np
ar=np.array([1.57079, 2.0943, 2.792,3.1415])
ar_out=np.ones(ar.shape)
ar=np.round(ar,out=ar_out)
print(ar)
print(ar_out)
print(ar is ar_out) # True
Output
[2. 2. 3. 3.]
[2. 2. 3. 3.]
True
Rounding off to nearest 100
By using negative decimal places we can round off to nearest hundred or thousands
import numpy as np
ar=np.array([435, 478, 1020,1089,22348])
print(np.round(ar,decimals=-2))
Output
[ 400 500 1000 1100 22300]
Rounding off to nearest 1000
import numpy as np
ar=np.array([435, 478, 1020,1889,22348])
print(np.round(ar,decimals=-3))