Python NumPy: How to Round NumPy Array Elements to the Nearest Integer in Python
When processing numerical data with NumPy, you'll often need to round floating-point values to the nearest whole number - whether for data binning, pixel value manipulation in image processing, converting continuous predictions to discrete categories, or simply cleaning up output for display. NumPy provides the np.rint() function specifically for this purpose.
In this guide, you'll learn how to use np.rint() and alternative methods to round array elements to the nearest integer, understand NumPy's rounding behavior, and choose the right function for your specific needs.
Using np.rint() - Round to Nearest Integer
The np.rint() function rounds each element of an array to the nearest integer, returning the result as a floating-point array:
numpy.rint(x, out=None)
Parameters:
| Parameter | Description |
|---|---|
x | Input array |
out | Optional pre-allocated output array |
Returns: An array with each element rounded to the nearest integer (as floats).
Rounding Positive Values
import numpy as np
arr = np.array([0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 1.8, 2.9])
rounded = np.rint(arr)
print("Original:", arr)
print("Rounded: ", rounded)
Output:
Original: [0.2 0.3 0.4 0.5 0.6 0.7 1.8 2.9]
Rounded: [0. 0. 0. 0. 1. 1. 2. 3.]
Rounding Negative Values
np.rint() handles negative numbers correctly, rounding them to the nearest integer:
import numpy as np
arr = np.array([-0.2, 0.7, -1.4, -4.5, -7.6, -19.7])
rounded = np.rint(arr)
print("Original:", arr)
print("Rounded: ", rounded)
Output:
Original: [ -0.2 0.7 -1.4 -4.5 -7.6 -19.7]
Rounded: [ -0. 1. -1. -4. -8. -20.]
Rounding 2D Arrays
np.rint() works element-wise on arrays of any dimension:
import numpy as np
arr_2d = np.array([[1.2, 2.7, 3.5],
[4.1, 5.9, 6.4]])
rounded = np.rint(arr_2d)
print("Original:")
print(arr_2d)
print("\nRounded:")
print(rounded)
Output:
Original:
[[1.2 2.7 3.5]
[4.1 5.9 6.4]]
Rounded:
[[1. 3. 4.]
[4. 6. 6.]]
Understanding Banker's Rounding (Half-Even)
Like np.round(), np.rint() uses banker's rounding - when a value is exactly halfway between two integers, it rounds to the nearest even number:
import numpy as np
arr = np.array([0.5, 1.5, 2.5, 3.5, 4.5, 5.5])
rounded = np.rint(arr)
print("Values: ", arr)
print("Rounded:", rounded)
Output:
Values: [0.5 1.5 2.5 3.5 4.5 5.5]
Rounded: [0. 2. 2. 4. 4. 6.]
Notice that 0.5 rounds to 0 (even), 1.5 rounds to 2 (even), and 2.5 rounds to 2 (even). This is the IEEE 754 standard and prevents systematic upward bias that would occur if halfway values always rounded up. Over large datasets, this produces more statistically accurate results.
Converting to Integer Data Type
np.rint() returns floats, not integers. If you need actual integer values, convert the result using .astype():
import numpy as np
arr = np.array([1.2, 2.7, 3.5, 4.1, 5.9])
# Round and convert to integers
rounded_int = np.rint(arr).astype(int)
print("Rounded (float):", np.rint(arr))
print("Rounded (int): ", rounded_int)
print("Dtype: ", rounded_int.dtype)
Output:
Rounded (float): [1. 3. 4. 4. 6.]
Rounded (int): [1 3 4 4 6]
Dtype: int64
If you need integer output, always round first, then convert. Don't use .astype(int) directly on unrounded floats - it truncates toward zero instead of rounding:
import numpy as np
arr = np.array([1.7, 2.3, -1.7])
print("astype(int) only: ", arr.astype(int)) # astype(int) only: [ 1 2 -1]
print("rint + astype(int): ", np.rint(arr).astype(int)) # rint + astype(int): [ 2 2 -2]
Alternative Rounding Methods
NumPy provides several functions that round to integers but with different behaviors:
import numpy as np
arr = np.array([1.7, 2.3, -1.7, -2.3])
print("Original:", arr)
print("rint: ", np.rint(arr)) # Nearest integer (banker's rounding)
print("round: ", np.round(arr)) # Same as rint for 0 decimals
print("floor: ", np.floor(arr)) # Always round down (toward -∞)
print("ceil: ", np.ceil(arr)) # Always round up (toward +∞)
print("trunc: ", np.trunc(arr)) # Always round toward zero
print("fix: ", np.fix(arr)) # Same as trunc
Output:
Original: [ 1.7 2.3 -1.7 -2.3]
rint: [ 2. 2. -2. -2.]
round: [ 2. 2. -2. -2.]
floor: [ 1. 2. -2. -3.]
ceil: [ 2. 3. -1. -2.]
trunc: [ 1. 2. -1. -2.]
fix: [ 1. 2. -1. -2.]
| Function | Behavior | 1.7 | -1.7 | 2.5 |
|---|---|---|---|---|
np.rint() | Nearest integer (half-even) | 2 | -2 | 2 |
np.round() | Same as rint for 0 decimals | 2 | -2 | 2 |
np.floor() | Always round down (toward -∞) | 1 | -2 | 2 |
np.ceil() | Always round up (toward +∞) | 2 | -1 | 3 |
np.trunc() | Always round toward zero | 1 | -1 | 2 |
np.rint() vs np.round()
When rounding to the nearest integer (0 decimal places), np.rint() and np.round() produce identical results:
import numpy as np
arr = np.array([1.4, 2.5, 3.6, -1.5])
print("rint: ", np.rint(arr))
print("round: ", np.round(arr))
print("Equal: ", np.array_equal(np.rint(arr), np.round(arr)))
Output:
rint: [ 1. 2. 4. -2.]
round: [ 1. 2. 4. -2.]
Equal: True
The key difference is that np.round() accepts a decimals parameter for rounding to arbitrary decimal places, while np.rint() always rounds to the nearest integer.
| Feature | np.rint() | np.round() |
|---|---|---|
| Round to nearest integer | ✅ | ✅ |
| Round to N decimal places | ❌ | ✅ (decimals=N) |
| Round to nearest 10/100 | ❌ | ✅ (decimals=-1) |
Use np.rint() when you specifically need nearest-integer rounding - its name makes the intent clearer. Use np.round() when you need more flexibility.
Common Mistake: Expecting "Round Half Up" Behavior
Many people expect 0.5 to always round up to 1, but NumPy uses banker's rounding:
import numpy as np
# ❌ Unexpected: 0.5 rounds to 0, not 1
result = np.rint(np.array([0.5]))
print(f"np.rint(0.5) = {result[0]}") # 0.0, not 1.0
Output:
np.rint(0.5) = 0.0
If you need traditional "round half up" behavior:
import numpy as np
def round_half_up(arr):
"""Round to nearest integer, with 0.5 rounding up."""
return np.floor(arr + 0.5)
arr = np.array([0.5, 1.5, 2.5, 3.5])
print("Banker's (rint): ", np.rint(arr))
print("Round half up: ", round_half_up(arr))
Output:
Banker's (rint): [0. 2. 2. 4.]
Round half up: [1. 2. 3. 4.]
Practical Example: Rounding Pixel Values
A common real-world use of np.rint() is rounding pixel values in image processing:
import numpy as np
# Simulated image with floating-point pixel values (0-255 range)
pixels = np.array([[127.3, 200.8, 55.1],
[0.4, 128.5, 254.9]])
# Round to nearest integer and convert to uint8
rounded_pixels = np.rint(pixels).astype(np.uint8)
print("Original pixels:")
print(pixels)
print("\nRounded pixels (uint8):")
print(rounded_pixels)
Output:
Original pixels:
[[127.3 200.8 55.1]
[ 0.4 128.5 254.9]]
Rounded pixels (uint8):
[[127 201 55]
[ 0 128 255]]
Summary
| Task | Function | Example |
|---|---|---|
| Round to nearest integer | np.rint(arr) | [1.7, 2.3] → [2., 2.] |
| Round and get int type | np.rint(arr).astype(int) | [1.7, 2.3] → [2, 2] |
| Round with decimals control | np.round(arr, decimals=N) | More flexible |
| Always round down | np.floor(arr) | [1.7] → [1.] |
| Always round up | np.ceil(arr) | [1.3] → [2.] |
To round NumPy array elements to the nearest integer, use np.rint() for clear, intent-revealing code. Remember that it uses banker's rounding (half-even), returns float values by default (chain .astype(int) if you need integers), and works element-wise on arrays of any dimension.