Python Practice › NumPy Programs › Program 96

Round to two decimals

EasyNumPy Programs · 96 of 100

Write a NumPy program to round elements to two decimal places.

Advertisement
Input
[4.8757, 2.4578]
Output
[4.88 2.46]

Approach

  1. np.round(a, 2).

Solution

Python
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.

More numpy programs

← All 100 NumPy Programs

Leave a Comment