Python Practice › NumPy Programs › Program 96
Round to two decimals
Write a NumPy program to round elements to two decimal places.
Input
[4.8757, 2.4578]
Output
[4.88 2.46]
Approach
np.round(a, 2).
Solution
import numpy as npprint(np.round(np.array([4.8757, 2.4578]), 2))Output
[4.88 2.46]
Note
Rounding gives 4.88 and 2.46; the original sample (4.86, 2.45) was not rounded correctly.
Learn the concept
New to this topic? Read the lesson, then come back.