/Curriculum/IntermediateMath Operations

30. NumPy Rounding FunctionsBeginner

Master round(), around(), floor(), ceil(), trunc(), and fix() for precision control.

10 mins

Concept Overview

NumPy provides comprehensive rounding functions for engineering and financial precision: `np.floor` rounds down, `np.ceil` rounds up, and `np.round` rounds to decimal places.

Hardware Mental Model

Floor is dropping down to the ground; Ceil is reaching up to the ceiling; Trunc is slicing off the decimal tail without looking at rounding.

Key Concepts (1)Click snippet to load in editor

`np.round(arr, decimals=N)` rounds to N decimal places using standard round-half-to-even bankers rounding.

r = np.round(arr, decimals=2)

⚠ Common Mistakes & Pitfalls

Avoid these frequent beginner syntax and logic traps
❌ Incorrect:np.round(1.555, 2) # Python scalar round vs NumPy array round
✓ Correct:np.round(np.array([1.555]), 2)

NumPy's `np.round()` works on both scalars and entire arrays simultaneously.

Pro Tip: Use `np.round(arr, decimals=2)` for financial calculations.

📌 Quick Revision

Core takeaway points from this topic
`np.round(arr, decimals=N)`: Rounds to N decimal places.
`np.floor(arr)`: Rounds down to nearest integer.
`np.ceil(arr)`: Rounds up to nearest integer.
`np.trunc(arr)`: Truncates decimal digits towards zero.
Editor: 30. NumPy Rounding Functions
Python ● Ready
Initializing Python IDE...
Ctrl+Enter
Execution Result
Click "RUN CODE" to execute and inspect array state

🎯 Try It Yourself: Practice Challenge

Hands-on Mode

Solve the objective below using NumPy vectorized syntax

Objective:Round to 1 Decimal Place

Create `arr = np.array([1.23, 4.56, 7.89])`, round to 1 decimal place using `np.round(arr, decimals=1)`, and print.

Expected Output Target:[1.2 4.6 7.9]
Practice Workspace
Python ● Ready
Initializing Python IDE...
Ctrl+Enter
Execution Result
Click "RUN CODE" to execute and inspect array state