NumPy round(): Decimal Places and Practical Reporting

np.round() rounds array values to the requested number of decimal places. Positive decimals select places after the decimal point, negative decimals select tens, hundreds or thousands, and exact halfway values round to the nearest even result.

ParameterMeaning
aInput array or array-like values
decimals=0Integer number of decimal places; negative values round left of the decimal point
out=NoneOptional destination with the same shape as the result

Round to Whole-Number Values

np.round rounds elementwise. Floating input remains floating even with zero decimal places. It does not convert degrees to radians or perform a unit conversion. Unless out is supplied, a new result is created.

import numpy as np
from decimal import Decimal, ROUND_HALF_UP

ar = np.array([1.57079, 2.0943, 2.792, 3.1415])
result = np.round(ar)
print("Original:", ar)
print("Rounded:", result)
np.testing.assert_array_equal(result, [2, 2, 3, 3])
assert result.dtype.kind == 'f'
assert not np.shares_memory(ar, result)

Expected output

Original: [1.57079 2.0943  2.792   3.1415 ]
Rounded: [2. 2. 3. 3.]

Choose Two or Three Decimal Places

decimals specifies places after the decimal point. It is not a number of significant figures. Positional and keyword forms give the same result.

ar = np.array([1.57079, 2.0943, 2.792, 3.1415])
print("Two places:", np.round(ar, 2))
print("Three places:", np.round(ar, decimals=3))
np.testing.assert_allclose(np.round(ar, 2), [1.57, 2.09, 2.79, 3.14])
np.testing.assert_allclose(np.round(ar, 3), [1.571, 2.094, 2.792, 3.142])

Expected output

Two places: [1.57 2.09 2.79 3.14]
Three places: [1.571 2.094 2.792 3.142]

Halfway Values Round to the Nearest Even Result

For exact halfway values, NumPy uses ties-to-even rather than always rounding up. This is easiest to see with halves, which binary floating point can represent exactly. Negative values follow the same nearest-even rule.

values = np.array([-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5, 4.5])
result = np.round(values)
print(result)
np.testing.assert_array_equal(result, [-4, -2, -2, 0, 0, 2, 2, 4, 4])

Expected output

[-4. -2. -2. -0.  0.  2.  2.  4.  4.]

Negative decimals Round to Hundreds

decimals=-2 rounds two positions to the left of the decimal point. Integer arrays remain integer arrays; values near a midpoint follow the same even-result rule.

ar = np.array([435, 478, 1020, 1089, 22348])
result = np.round(ar, decimals=-2)
print(result)
np.testing.assert_array_equal(result, [400, 500, 1000, 1100, 22300])

Expected output

[  400   500  1000  1100 22300]

Round to Thousands

decimals=-3 selects thousands. Rounding a small positive number to thousands can produce zero; that is a rounded representation, not evidence that the original measurement was zero.

ar = np.array([435, 478, 1020, 1889, 22348])
result = np.round(ar, decimals=-3)
print(result)
np.testing.assert_array_equal(result, [0, 0, 1000, 2000, 22000])

Expected output

[    0     0  1000  2000 22000]

Store Results in out and Check Identity

The destination must match the result shape and support the stored values. The function returns the supplied destination. Unlike default rounding, out can overwrite an existing array. See shape and ones().

ar = np.array([1.57079, 2.0943, 2.792, 3.1415])
ar_out = np.ones(ar.shape)
returned = np.round(ar, out=ar_out)
print(returned)
print(ar_out)
print("Returned destination:", returned is ar_out)
assert returned is ar_out
np.testing.assert_array_equal(ar, [1.57079, 2.0943, 2.792, 3.1415])

Expected output

[2. 2. 3. 3.]
[2. 2. 3. 3.]
Returned destination: True

Round a 2D Array without Changing Shape

Rounding does not reduce an axis, unlike mean or sum. The ndarray round method and np.around alias offer the same rounding operation.

a = np.array([[1.234, 2.345], [3.456, 4.567]])
result = a.round(2)
print(result)
print("Shape:", result.shape)
assert result.shape == a.shape
np.testing.assert_array_equal(result, np.around(a, 2))

Expected output

[[1.23 2.35]
 [3.46 4.57]]
Shape: (2, 2)

Binary Floats and Scaling Can Affect Decimal Rounding

Many decimal fractions have no exact binary representation. NumPy also uses a fast scaling-based rounding algorithm that can introduce small errors. NumPy round and Python round need not agree on every float. A stored value near a decimal midpoint may not be an exact tie.

value = 16.055
print("Stored float:", format(value, '.17g'))
print("NumPy round:", np.round(value, 2))
print("Python round:", round(value, 2))
print("Exact decimal with half-up:", Decimal('16.055').quantize(Decimal('0.01'), rounding=ROUND_HALF_UP))
assert np.isclose(np.round(value, 2), 16.06)

Expected output

Stored float: 16.055
NumPy round: 16.06
Python round: 16.05
Exact decimal with half-up: 16.06

Formatting Controls Display without Changing Data

Use a format specification when the requirement is to display exactly two decimal places. np.round does not guarantee trailing zeros in a printed representation. Formatted values are strings, so keep the numeric source for calculations.

a = np.array([1.0, 2.5, 3.456])
labels = [f'{value:.2f}' for value in a]
print("Rounded numeric values:", np.round(a, 2))
print("Display strings:", labels)
assert labels == ['1.00', '2.50', '3.46']
assert a[2] == 3.456

Expected output

Rounded numeric values: [1.   2.5  3.46]
Display strings: ['1.00', '2.50', '3.46']

Temporary Print Options Do Not Round Stored Values

A local printoptions context affects how arrays are displayed. It does not alter the underlying measurements or change calculations after the context ends.

a = np.array([1.234567, 8.765432])
before = a.copy()
with np.printoptions(precision=2, floatmode='fixed'):
    print("Displayed:", a)
print("Still stored:", a)
np.testing.assert_array_equal(a, before)

Expected output

Displayed: [1.23 8.77]
Still stored: [1.234567 8.765432]

Compare round, rint, floor, ceil and trunc

round with zero decimals and rint choose nearest integers using ties-to-even. floor moves toward negative infinity, ceil toward positive infinity and trunc toward zero. Integer casting also truncates finite in-range floats; it is not nearest rounding. See conversion and bounds.

a = np.array([-2.7, -2.5, -2.1, 2.1, 2.5, 2.7])
print("round:", np.round(a))
print("rint:", np.rint(a))
print("floor:", np.floor(a))
print("ceil:", np.ceil(a))
print("trunc:", np.trunc(a))
print("Integer cast:", a.astype(np.int32))
assert np.floor(a[0]) == -3 and np.trunc(a[0]) == -2

Expected output

round: [-3. -2. -2.  2.  2.  3.]
rint: [-3. -2. -2.  2.  2.  3.]
floor: [-3. -3. -3.  2.  2.  2.]
ceil: [-2. -2. -2.  3.  3.  3.]
trunc: [-2. -2. -2.  2.  2.  2.]
Integer cast: [-2 -2 -2  2  2  2]

NaN, Infinity and Complex Inputs

Rounding preserves NaN and infinities. It does not repair missing or invalid measurements. For complex arrays, real and imaginary components are rounded separately.

a = np.array([1.234, np.nan, np.inf, -np.inf])
print("Special values:", np.round(a, 2))
z = np.array([1.234 + 2.345j, -3.456 + 4.567j])
print("Complex:", np.round(z, 2))
assert np.isnan(np.round(a, 2)[1])
np.testing.assert_allclose(np.round(z, 2), [1.23 + 2.35j, -3.46 + 4.57j])

Expected output

Special values: [1.23  nan  inf -inf]
Complex: [ 1.23+2.35j -3.46+4.57j]

Round at the Stage Required by the Reporting Rule

Rounding each item before summing can differ from rounding the final sum. This synthetic example makes that distinction clear. Preserve raw data and state whether the report sums rounded items or rounds a raw total. See sum().

values = np.array([0.004, 0.004, 0.004])
rounded_items_total = np.round(values, 2).sum()
rounded_final_total = np.round(values.sum(), 2)
print("Sum of rounded items:", rounded_items_total)
print("Rounded raw total:", rounded_final_total)
assert rounded_items_total == 0
np.testing.assert_allclose(rounded_final_total, 0.01)

Expected output

Sum of rounded items: 0.0
Rounded raw total: 0.01

Use Decimal for an Explicit Decimal Half-Up Rule

When a specification requires exact decimal arithmetic and a stated tie rule, construct Decimal from the original text. Constructing it from an already rounded float cannot recover the source decimal. This synthetic calculation uses an explicit half-up rule; choose the rule required by your application.

source_text = ['1.005', '2.675', '-1.005']
rounded = [Decimal(text).quantize(Decimal('0.01'), rounding=ROUND_HALF_UP) for text in source_text]
print("Half-up decimal values:", [str(value) for value in rounded])
assert [str(value) for value in rounded] == ['1.01', '2.68', '-1.01']

Expected output

Half-up decimal values: ['1.01', '2.68', '-1.01']

Practical Measurement Report: Preserve Raw Values and Missing Data

These synthetic sensor readings are displayed to two places, while the average is calculated from finite raw values. Missing entries receive a separate display label. Reporting precision does not establish instrument accuracy.

readings = np.array([12.344, 12.346, np.nan, 12.351])
valid = np.isfinite(readings)
labels = [f'{value:.2f}' if ok else 'Missing' for value, ok in zip(readings, valid)]
mean_raw = readings[valid].mean() if valid.any() else np.nan
print("Display:", labels)
print("Finite count:", valid.sum())
print("Raw-data mean displayed:", f'{mean_raw:.2f}')
assert labels == ['12.34', '12.35', 'Missing', '12.35']
assert np.isnan(readings[2]) and valid.sum() == 3

Expected output

Display: ['12.34', '12.35', 'Missing', '12.35']
Finite count: 3
Raw-data mean displayed: 12.35

Common Rounding Mistakes

decimals counts decimal places, not significant digits. Ties-to-even is not always half-up. A rounded float remains a float and may not print trailing zeros. Display formatting does not change stored data. Rounding after every intermediate operation can change later results. Avoid extreme decimal settings or magnitudes without checking precision and dtype limits. Use tolerance checks for approximate numerical results, rather than treating rounding as proof of exact equality.

Exercise: Round Readings and Whole-Number Ties

Round [1.234, 2.346, NaN, -1.234] to two places and create display labels with Missing for the unknown reading. Separately round [1.5, 2.5, 3.5, 4.5] to whole-number values. Predict [2, 2, 4, 4] for those exact ties.

Exercise Solution

Keep the original readings separate from the rounded result. Use a validity check for the displayed missing value.

readings = np.array([1.234, 2.346, np.nan, -1.234])
rounded = np.round(readings, 2)
labels = [f'{value:.2f}' if np.isfinite(value) else 'Missing' for value in rounded]
ties = np.round([1.5, 2.5, 3.5, 4.5])
print("Rounded:", rounded)
print("Display:", labels)
print("Ties:", ties)
np.testing.assert_allclose(rounded, [1.23, 2.35, np.nan, -1.23], equal_nan=True)
assert labels == ['1.23', '2.35', 'Missing', '-1.23']
np.testing.assert_array_equal(ties, [2, 2, 4, 4])

Expected output

Rounded: [ 1.23  2.35   nan -1.23]
Display: ['1.23', '2.35', 'Missing', '-1.23']
Ties: [2. 2. 4. 4.]

Practice in Google Colab

Open in Google Colab View on GitHub
All sample data is embedded. Run the examples and practise decimal places, ties and measurement reporting. Save a copy in Drive to keep your changes.

Continue with NumPy math functions, mean(), arange(), linspace() and Python round(). For angle conversion, see rad2deg(). Reference: NumPy round documentation.




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer