Rounding & Clipping
Rounding and clipping are the tools for tidying numeric arrays — rounding controls how many decimals you keep, while clipping caps every value into a valid range so outliers cannot escape.
Learn Rounding & Clipping in our free NumPy course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Numpy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
You'll round to decimals, floor and ceil to integers, truncate toward zero, clip values into bounds, and read signs with np.sign.
np.round(arr, decimals) rounds to the nearest value at the chosen number of decimal places ( np.around is the same function). One quirk: NumPy uses banker's rounding — exact halves round to the nearest even number, so 0.5 rounds to 0 and 2.5 rounds to 2 .
These three always go to an integer but in different directions. np.floor rounds down , np.ceil rounds up , and np.trunc chops off the fraction toward zero . The difference only shows on negative numbers.
np.clip(arr, low, high) caps every value into a range: anything below low becomes low , anything above high becomes high . It is the cleanest way to limit outliers. np.sign returns -1 , 0 , or 1 to capture the direction of each value.
Replace ___ with the bounds that keep every value between 0 and 10.
Answer: 0, 10 — values below 0 become 0 and values above 10 become 10.
NumPy uses banker's rounding, sending halves to the even number:
✅ Fix: if you need always-up rounding, add a tiny offset or use np.floor(x + 0.5) deliberately.
✅ Fix: choose floor for "round down" and trunc for "drop the fraction".
✅ Fix: pass the smaller bound first: np.clip(arr, 0, 100) .
Round raw prices to 2 decimals, then clip any negative or sky-high values into a sensible 0 to 100 range.
Lesson complete — numbers tidied!
You can round to decimals with np.round , snap to integers with floor / ceil / trunc , cap outliers with np.clip , and read direction with np.sign .
🚀 Up next: Cumulative & Difference Functions — running totals and element-to-element changes.
Practice quiz
What does np.round([0.5, 1.5, 2.5, 3.5]) return?
NumPy uses banker's rounding, sending halves to the nearest even number.
What does np.floor(2.7) return?
- 3.0
- 2.7
- -3.0
- 2.0
Answer: 2.0. floor always rounds down to the integer below, giving 2.0.
What does np.ceil(2.3) return?
- 3.0
- 2.0
- 2.3
- -2.0
Answer: 3.0. ceil always rounds up to the integer above, giving 3.0.
What does np.floor(-2.7) return?
- -2.0
- 2.0
- -3.0
- 3.0
Answer: -3.0. floor rounds down (more negative), so -2.7 becomes -3.0.
What does np.trunc(-2.7) return?
- -3.0
- -2.0
- 3.0
- 2.0
Answer: -2.0. trunc drops the fraction toward zero, so -2.7 becomes -2.0.
What does np.clip([-5, 30, 75, 130], 0, 100) return?
Values below 0 become 0 and above 100 become 100.
What does np.sign([-8, 0, 4]) return?
sign returns -1 for negatives, 0 for zero, and 1 for positives.
What does np.clip(scores, 0, None) do?
- Clips both ends to 0
- Raises an error
- Floors at 0 with no upper limit
- Removes all values
Answer: Floors at 0 with no upper limit. None as the upper bound means clip only the lower side at 0.
After np.round with the default decimals=0, what type are the results?
- strings
- floats like 1.
- true integers
- booleans
Answer: floats like 1.. Rounding returns floats (1.), so cast with .astype(int) for integers.
When does np.floor differ from np.trunc?
- For very large numbers
- For zero
- Never
- Only for negative numbers
Answer: Only for negative numbers. They agree on positives; on negatives floor goes down while trunc goes toward zero.