ufunc Rounding Decimals
Floating-point numbers often carry more digits than you want, or need to be turned into whole numbers. NumPy has five main ways to round, and they differ in which direction they go:
| Function | Rule | 2.7 | −2.7 | 2.5 |
|---|---|---|---|---|
np.trunc / np.fix |
drop the decimals (toward zero) | 2 | −2 | 2 |
np.around / np.round |
nearest, ties to the even number | 3 | −3 | 2 |
np.floor |
down (toward −∞) | 2 | −3 | 2 |
np.ceil |
up (toward +∞) | 3 | −2 | 3 |
np.rint |
nearest integer, ties to even | 3 | −3 | 2 |
All of them return floats (3. not 3). Convert with .astype(int) if you need an integer array.
Truncation
import numpy as np
values = np.array([-3.1666, 3.6667, -0.5, 0.5])
print(np.trunc(values))
print(np.fix(values)) # same thing[-3. 3. -0. 0.]
[-3. 3. -0. 0.]
Rounding to a number of decimals
np.around(a, decimals) (and its alias np.round, and the array method a.round()) rounds to the given number of decimal places. A negative decimals rounds to tens, hundreds and so on:
import numpy as np
pi_ish = np.array([3.14159, 2.71828, 1.41421])
print(np.around(pi_ish, 2))
print(pi_ish.round(3))
population = np.array([18_734, 245_210, 1_499])
print(np.round(population, -3)) # nearest thousand[3.14 2.72 1.41]
[3.142 2.718 1.414]
[ 19000 245000 1000]
Floor and ceil
np.floor always rounds down and np.ceil always rounds up, which matters for negative numbers and for "how many do I need" questions:
import numpy as np
temps = np.array([-1.5, -0.2, 0.2, 1.5])
print(np.floor(temps))
print(np.ceil(temps))
# How many buses (50 seats each) do the groups need?
groups = np.array([120, 50, 51, 7])
print(np.ceil(groups / 50).astype(int))[-2. -1. 0. 1.]
[-1. -0. 1. 2.]
[3 1 2 1]
Banker's rounding: ties go to even
A surprise for many people: np.round(2.5) is 2.0, not 3.0. NumPy (like Python's round) uses round half to even: an exact tie goes to the nearest even number. Over many values this avoids a systematic upward bias:
import numpy as np
halves = np.array([0.5, 1.5, 2.5, 3.5, 4.5])
print(np.round(halves))
print("sum of rounded:", np.round(halves).sum(), " true sum:", halves.sum())
# Classic "round half up" for positive numbers, if your rules require it:
print(np.floor(halves + 0.5))[0. 2. 2. 4. 4.]
sum of rounded: 12.0 true sum: 12.5
[1. 2. 3. 4. 5.]
The true total is 12.5. Half-to-even rounding gives 12, because ties go down as often as they go up; always rounding halves up gives 15, a systematic overshoot.
Floating-point surprises
Some decimals cannot be represented exactly in binary, so a value that looks like a tie may be slightly above or below it. Python and NumPy can even disagree about it:
import numpy as np
print(f"{2.675:.20f}") # what is really stored
print(round(2.675, 2)) # Python rounds the stored value exactly
print(np.round(2.675, 2)) # NumPy scales by 100 first, then rounds
print(0.1 + 0.2, np.round(0.1 + 0.2, 10))2.67499999999999982236
2.67
2.68
0.30000000000000004 0.3
2.675 is really stored as 2.67499999…, so Python's round correctly gives 2.67. NumPy computes 2.675 * 100, which rounds to exactly 267.5 in floating point, and then rounds that tie to the even 268, giving 2.68. NumPy's approach is faster but can be off by one in the last decimal for such borderline values.
Tip: For money, avoid floats entirely: store amounts as integer cents, or use Python's
decimalmodule. For display, rounding with an f-string format such asf"{x:.2f}"is usually all you need.
Test yourself
What is np.floor(-2.3)?
- -2.0
- -3.0
- 2.0
- -2.3
What does np.round(3.5) return?
- 3.0
- 4.0
- 3
- 4
Which function always rounds toward zero?
- np.floor
- np.ceil
- np.trunc
- np.around