Saturday, September 6, 2025
HomeLanguagesHow to round array elements to the given number of decimals using...

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

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]
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32270 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6639 POSTS0 COMMENTS
Nicole Veronica
11803 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11869 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS