When working with numerical data, rounding is often necessary to control precision or present cleaner results. In Python, the NumPy library provides a suite of ufuncs (universal functions) specifically designed to perform decimal rounding efficiently on arrays.
In this article, you'll learn:
-
✅ What rounding ufuncs are in NumPy
-
Differences between each rounding method
-
How to use each rounding function with examples
-
Full code example
-
Tips and Common pitfalls
What is a ufunc in NumPy?
A ufunc (universal function) in NumPy operates element-wise on ndarray
objects. They are:
-
Fast – implemented in C under the hood
-
Broadcast-compatible
-
Vectorized – no explicit loops needed
For rounding, NumPy includes several built-in ufuncs to suit different use cases.
➕ NumPy Rounding Functions
Function | Description |
---|---|
np.around() |
Rounds to the nearest value (can specify decimals) |
np.round_() |
Same as around() , alias |
np.floor() |
Rounds down to the nearest lower integer |
np.ceil() |
Rounds up to the nearest higher integer |
np.trunc() |
Truncates the decimal part (toward zero) |
np.fix() |
Rounds toward zero (like trunc() for floats) |
Step-by-Step Examples
Let's look at how to use each of these rounding ufuncs with practical examples.
Setup
import numpy as np
arr = np.array([1.5, 2.3, -1.7, -2.9])
1. np.around()
(or np.round_()
)
Rounds to nearest integer by default. You can also round to specific decimal places.
print(np.around(arr))
# Output: [ 2. 2. -2. -3.]
print(np.around(arr, decimals=1))
# Output: [ 1.5 2.3 -1.7 -2.9]
2. np.floor()
Rounds down (toward negative infinity):
print(np.floor(arr))
# Output: [ 1. 2. -2. -3.]
3. np.ceil()
Rounds up (toward positive infinity):
print(np.ceil(arr))
# Output: [ 2. 3. -1. -2.]
4. np.trunc()
Truncates decimals (toward zero):
print(np.trunc(arr))
# Output: [ 1. 2. -1. -2.]
5. np.fix()
Rounds toward zero (very similar to trunc()
):
print(np.fix(arr))
# Output: [ 1. 2. -1. -2.]
✅ Full Working Example
import numpy as np
arr = np.array([3.6, 2.1, -1.5, -2.8])
print("Original:", arr)
print("Around (0 decimals):", np.around(arr))
print("Round to 1 decimal:", np.around(arr, decimals=1))
print("Floor:", np.floor(arr))
print("Ceil:", np.ceil(arr))
print("Trunc:", np.trunc(arr))
print("Fix:", np.fix(arr))
Output:
Original: [ 3.6 2.1 -1.5 -2.8]
Around (0 decimals): [ 4. 2. -2. -3.]
Round to 1 decimal: [ 3.6 2.1 -1.5 -2.8]
Floor: [ 3. 2. -2. -3.]
Ceil: [ 4. 3. -1. -2.]
Trunc: [ 3. 2. -1. -2.]
Fix: [ 3. 2. -1. -2.]
Tips
-
Performance: These functions are vectorized—no need to loop through arrays.
-
In-place Rounding: You can round and assign back using slicing:
arr[:] = np.around(arr, 2)
-
Decimals Argument:
np.around()
lets you round ton
decimal places.
Common Pitfalls
Pitfall | How to Avoid |
---|---|
Forgetting decimals argument |
Use decimals=n with np.around() |
Expecting fix() to round like round() |
Remember, fix() truncates toward 0 |
Confusing floor and trunc |
floor always goes down; trunc goes toward zero |
Conclusion
NumPy’s built-in rounding ufuncs make working with numeric precision simple, fast, and expressive. Whether you're rounding to integers, floors, ceilings, or truncating decimals, NumPy provides efficient tools for every use case.
By using ufuncs like np.around()
, np.floor()
, np.ceil()
, and np.trunc()
, you avoid loops and unlock the full power of NumPy’s vectorized operations.
What's Next?