Skip to content
Related Articles

Related Articles

How to round array elements to the given number of decimals using NumPy?

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 05 Sep, 2020

In NumPy, we can round array elements to the given number of decimals with the help of round(). 

Syntax:

np.round(a, decimals=0, out=None)

The first parameter will be an array and the second parameter will be the number of decimals for which needed rounded. If no parameter will be pass as the second parameter then by default it takes 0. It will return round array elements to the given number of decimals.

Example 1:

Python3




import numpy as np
  
  
# perform the numpy.round
rounded_array = np.round([1.5, 1.53, 1.23, 3.89])
  
# print the rounded_array
print(rounded_array)

Output

[2. 2. 1. 4.]

Example 2:

Python3




import numpy as np
  
  
# perform the numpy.round
rounded_array = np.round([1.5, 1.53, 1.23, 3.89], 
                         decimals=1)
  
# print the rounded_array
print(rounded_array)

Output:

[1.5 1.5 1.2 3.9]

Example

Python3




import numpy as np
  
  
# perform the numpy.round
rounded_array = np.round(
    [1.534, 1.5389, 1.2301, 3.89903, 6.987, 4.09], decimals=2)
  
# print the rounded_array
print(rounded_array)

Output:

[1.53 1.54 1.23 3.9  6.99 4.09]


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!