Chapter 4: Rounding Decimals

1. Why rounding decimals matters (and why NumPy makes it tricky)

When we work with floating-point numbers in NumPy, we almost always need to round them at some point:

  • For beautiful reports and tables (nobody wants 12.3456789012345 in output)
  • For display purposes (charts, dashboards, print statements)
  • For saving storage (float64 → float32 after rounding)
  • For comparison (rounded values are easier to match)
  • For model output post-processing (many APIs expect 2–4 decimal places)
  • For avoiding floating-point noise (0.30000000000000004 → 0.3)

But rounding is not trivial in NumPy because there are several functions, they behave slightly differently, and they interact with bankers’ rounding (round-to-even).

2. The four main rounding functions you need to know

NumPy gives you these four important rounding ufuncs:

Function What it does Returns Common use case Bankers’ rounding?
np.round() Round to nearest, ties to even (default) new array General-purpose rounding Yes
np.rint() Round to nearest integer, ties to even new array Same as round(…, decimals=0) Yes
np.floor() Always round down (towards -∞) new array Lower bound, flooring ages/prices No
np.ceil() Always round up (towards +∞) new array Upper bound, ceiling required items No
np.trunc() Truncate (remove decimal part, towards zero) new array Remove fractional part without rounding No

3. Let’s see them side by side – most important examples

Python

Typical output:

text

Important observations:

  • round and rint use round-to-even (bankers’ rounding)
    • 1.5 → 2, 2.5 → 2, 3.5 → 4, 4.5 → 4
  • floor always goes down (more negative for negatives)
  • ceil always goes up (more positive)
  • trunc just removes decimals (towards zero)

4. Rounding to specific number of decimal places

Python

Output highlights:

text

Student trap — decimals=-1 rounds to the nearest 10, -2 to nearest 100, etc.

5. Realistic patterns you will use again and again

Pattern 1 – Clean display values for reports

Python

Pattern 2 – Clip & round sensor readings

Python

Pattern 3 – Round probabilities / percentages

Python

Pattern 4 – Round before saving to database / CSV

Python

Summary – Rounding Functions Quick Reference

Function Behavior Use when you want…
np.round nearest, ties to even most general-purpose rounding
np.rint nearest integer, ties to even equivalent to round(…, decimals=0)
np.floor always down (towards -∞) lower bound, flooring
np.ceil always up (towards +∞) upper bound, ceiling
np.trunc cut off decimal part (towards zero) remove fractional part without rounding
np.around alias of np.round same as round

Final teacher advice (very important)

Golden rule #1 Use np.round for almost everything unless you have a specific reason to use floor/ceil/trunc.

Golden rule #2 When displaying numbers (print, report, chart labels) → round early (before saving/showing).

Golden rule #3 Be careful with negative numbers — floor and ceil behave opposite to intuition:

  • floor(-2.3) = -3 (goes more negative)
  • ceil(-2.3) = -2 (goes less negative)

Golden rule #4 np.round uses bankers’ rounding (ties round to nearest even) — this surprises people coming from Excel or school.

Would you like to continue with any of these next?

  • Rounding pitfalls with floating-point precision
  • How to round to significant figures (not just decimals)
  • Rounding before/after aggregation (mean, sum, etc.)
  • Realistic mini-project: clean & round financial / sensor data
  • Difference between round / rint / around

Just tell me what you want to focus on next! 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *