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.
import numpy as np
arr = np.array([1.234, 5.678, 9.999])
print(np.round(arr, 2))2Practical Example
Here is a real-world application of np.round() showing how it is used in production NumPy code.
import numpy as np
arr = np.array([0.5, 1.5, 2.5, 3.5])
print(np.round(arr))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.
import numpy as np
arr = np.array([1.234, 5.678, 9.999])
print(np.round(arr, 2))