# round()

Numpy Math Functions

 a Input array with degree out Optional , ndarray to store result

## Examples

We will use the options in our sample scripts.
import numpy as np
ar=np.array([1.57079, 2.0943,  2.792,3.1415])
print(np.round(ar))
Output
[2. 2. 3. 3.]

## decimals

We can add decimals=2 to specify the number of places we want the rounding off.
ar=np.array([1.57079, 2.0943,  2.792,3.1415])
print(np.round(ar,2))
Output
[1.57 2.09 2.79 3.14]
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))
Output
[    0     0  1000  2000 22000]

Subscribe to our YouTube Channel here

## Subscribe

* indicates required
Subscribe to plus2net

plus2net.com