w3resource logo
Python Exercises

Python NumPy: Round array elements to the given number of decimals

Python NumPy Math, Statistics, Trigonometry and Linear algebra: Exercise-8 with Solution

Write a Python program to round array elements to the given number of decimals.

Sample Solution:-

Python Code:

import numpy as np
x = np.round([1.45, 1.50, 1.55])
print(x)
x = np.round([0.28, .50, .64], decimals=1)
print(x)
x = np.round([.5, 1.5, 2.5, 3.5, 4.5]) # rounds to nearest even value
print(x)

Sample Output:

[ 1.  2.  2.]                                                          
[ 0.3  0.5  0.6]                                                       
[ 0.  2.  2.  4.  4.]

Python Code Editor:

Improve this sample solution and post your code through Disqus