🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.round()

AI & DATA SCIENCE // np-round

np.round() rounds every element of an array to a given number of decimal places, using the same round-half-to-even convention as Python's built-in round().

Syntax

np.round(arr, decimals=0)

Deep Dive Course

np.round(arr, n) rounds each element to n decimal places, 0 by default, rounding to whole numbers, using round-half-to-even for exact ties, the same banker's rounding rule Python's own round() uses — so rounding 2.5 to zero decimals gives 2, not 3. Passing a negative value for decimals rounds to the left of the decimal point instead, so rounding 1234 to -2 decimals rounds to the nearest hundred, giving 1200.

1Understanding np.round()

np.round(arr, n) rounds each element to n decimal places, 0 by default, rounding to whole numbers, using round-half-to-even for exact ties, the same banker's rounding rule Python's own round() uses — so rounding 2.5 to zero decimals gives 2, not 3. Passing a negative value for decimals rounds to the left of the decimal point instead, so rounding 1234 to -2 decimals rounds to the nearest hundred, giving 1200.

💡

Like Python's round(), np.round() uses round-half-to-even, so don't assume every .5 value rounds up — verify the behavior on ties if your calculation specifically depends on 'always round half up' semantics, and use a manual adjustment if that specific behavior is required.

editor.html
import numpy as np

arr = np.array([1.234, 5.678, 9.999])
print(np.round(arr, 2))
localhost:3000

2Practical Example

Here is a real-world application of np.round() showing how it is used in production NumPy code.

editor.html
import numpy as np

arr = np.array([0.5, 1.5, 2.5, 3.5])
print(np.round(arr))
localhost:3000

3Best Practices

Follow these guidelines when working with np.round():

1. Use np.round() for display/output formatting rather than for values that will be compared for exact equality afterward, since results remain floats with their usual precision quirks

2. Use negative decimals values to round to tens, hundreds, or other larger place values, instead of manually dividing and multiplying

3. Verify round-half-to-even behavior explicitly if your specific use case assumes traditional 'round half up' rounding on exact ties

⚠️

Tip: Like Python's round(), np.round() uses round-half-to-even, so don't assume every .5 value rounds up — verify the behavior on ties if your calculation specifically depends on 'always round half up' semantics, and use a manual adjustment if that specific behavior is required.

editor.html
import numpy as np

arr = np.array([1.234, 5.678, 9.999])
print(np.round(arr, 2))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.array([1.234, 5.678, 9.999])
print(np.round(arr, 2))
Example 02Advanced Example
import numpy as np

arr = np.array([0.5, 1.5, 2.5, 3.5])
print(np.round(arr))

Best Practices

  • Use np.round() for display/output formatting rather than for values that will be compared for exact equality afterward, since results remain floats with their usual precision quirks
  • Use negative decimals values to round to tens, hundreds, or other larger place values, instead of manually dividing and multiplying
  • Verify round-half-to-even behavior explicitly if your specific use case assumes traditional 'round half up' rounding on exact ties

Interview Question

Why does np.round() on the array [0.5, 1.5, 2.5, 3.5] produce [0, 2, 2, 4] instead of [1, 2, 3, 4]?

Hint: Think about the specific rounding rule applied to exact ties.

np.round() uses round-half-to-even, the same convention as Python's built-in round(): when a value is exactly halfway between two integers, it rounds to whichever of the two is even, rather than always rounding up. That's why 0.5 rounds to 0, 1.5 rounds to 2, 2.5 rounds to 2, not 3, and 3.5 rounds to 4 — this convention reduces statistical bias when rounding many values, compared to always rounding ties in the same direction, but it surprises people expecting the traditional 'round half up' rule taught in school.

Exercises

MediumPractice using np.round() in a real scenario.
View Solution
import numpy as np

arr = np.array([1.234, 5.678, 9.999])
print(np.round(arr, 2))

Frequently Asked Questions

Why does np.round() on the array [0.5, 1.5, 2.5, 3.5] produce [0, 2, 2, 4] instead of [1, 2, 3, 4]?

np.round() uses round-half-to-even, the same convention as Python's built-in round(): when a value is exactly halfway between two integers, it rounds to whichever of the two is even, rather than always rounding up. That's why 0.5 rounds to 0, 1.5 rounds to 2, 2.5 rounds to 2, not 3, and 3.5 rounds to 4 — this convention reduces statistical bias when rounding many values, compared to always rounding ties in the same direction, but it surprises people expecting the traditional 'round half up' rule taught in school.

Related Functions

round()np-clipnp-absolute