Skip to content

Python NumPy: How to Round Array Elements to a Given Number of Decimals Using NumPy

Learn how to use NumPy's np.round() function to round array elements to any number of decimals, handle negative decimals for whole-number precision, and understand banker's rounding behavior for accurate numerical results.

When working with numerical data in Python, you'll often encounter floating-point numbers with many decimal places - whether from scientific measurements, financial calculations, or machine learning outputs. Rounding these values to a specific number of decimals improves readability, ensures consistent formatting, and can reduce floating-point noise in your results.

In this guide, you'll learn how to use NumPy's np.round() function to round array elements, understand its parameters, and handle edge cases like negative rounding and banker's rounding.

Understanding np.round()

NumPy provides the np.round() function (also available as np.around()) to round array elements to a specified number of decimal places:

np.round(a, decimals=0, out=None)

Parameters:

Parameter Description
a Input array or scalar to round
decimals Number of decimal places to round to (default: 0)
out Optional pre-allocated output array

Returns: An array with elements rounded to the specified number of decimals.

Rounding to Whole Numbers (Default)

When no decimals parameter is specified, np.round() rounds to the nearest integer:

import numpy as np

arr = np.array([1.5, 2.3, 3.7, 4.1, 5.9])

rounded = np.round(arr)
print("Original:", arr)
print("Rounded: ", rounded)

Output:

Original: [1.5 2.3 3.7 4.1 5.9]
Rounded:  [2. 2. 4. 4. 6.]

Rounding to a Specific Number of Decimals

One Decimal Place

import numpy as np

arr = np.array([1.5, 1.53, 1.23, 3.89, 6.987])

rounded = np.round(arr, decimals=1)
print("Original:", arr)
print("Rounded: ", rounded)

Output:

Original: [1.5   1.53  1.23  3.89  6.987]
Rounded:  [1.5 1.5 1.2 3.9 7. ]

Two Decimal Places

import numpy as np

arr = np.array([1.534, 1.5389, 1.2301, 3.89903, 6.987, 4.09])

rounded = np.round(arr, decimals=2)
print("Original:", arr)
print("Rounded: ", rounded)

Output:

Original: [1.534   1.5389  1.2301  3.89903 6.987   4.09   ]
Rounded:  [1.53 1.54 1.23 3.9  6.99 4.09]

Three Decimal Places

import numpy as np

arr = np.array([3.14159, 2.71828, 1.41421])

rounded = np.round(arr, decimals=3)
print("Rounded to 3 decimals:", rounded)

Output:

Rounded to 3 decimals: [3.142 2.718 1.414]

Rounding with Negative Decimals

Setting decimals to a negative number rounds to the left of the decimal point - to the nearest ten, hundred, thousand, etc.:

import numpy as np

arr = np.array([1234, 5678, 9012, 345, 67])

print("To nearest 10:  ", np.round(arr, decimals=-1))
print("To nearest 100: ", np.round(arr, decimals=-2))
print("To nearest 1000:", np.round(arr, decimals=-3))

Output:

To nearest 10:   [1230 5680 9010  340   70]
To nearest 100:  [1200 5700 9000  300  100]
To nearest 1000: [1000 6000 9000    0    0]

This is useful for simplifying large numbers in reports or visualizations.

Rounding 2D Arrays

np.round() works with multi-dimensional arrays just as well:

import numpy as np

arr_2d = np.array([[1.234, 5.678],
                    [9.012, 3.456]])

rounded = np.round(arr_2d, decimals=1)
print("Original:")
print(arr_2d)
print("\nRounded to 1 decimal:")
print(rounded)

Output:

Original:
[[1.234 5.678]
 [9.012 3.456]]

Rounded to 1 decimal:
[[1.2 5.7]
 [9.  3.5]]

Understanding Banker's Rounding (Half-Even)

NumPy uses banker's rounding (also called "round half to even") by default. When a value is exactly halfway between two possible rounded values, it rounds to the nearest even number:

import numpy as np

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

rounded = np.round(arr)
print("Values:  ", arr)
print("Rounded: ", rounded)

Output:

Values:   [0.5 1.5 2.5 3.5 4.5]
Rounded:  [0. 2. 2. 4. 4.]

If you need traditional "round half up" behavior, you can implement it manually:

import numpy as np

def round_half_up(arr, decimals=0):
    multiplier = 10 ** decimals
    return np.floor(arr * multiplier + 0.5) / multiplier

arr = np.array([0.5, 1.5, 2.5, 3.5, 4.5])
print("Banker's rounding:", np.round(arr))
print("Round half up:    ", round_half_up(arr))

Output:

Banker's rounding: [0. 2. 2. 4. 4.]
Round half up:     [1. 2. 3. 4. 5.]

Alternative Functions: floor(), ceil(), and trunc()

NumPy offers additional rounding functions for specific behaviors:

import numpy as np

arr = np.array([1.7, 2.3, -1.7, -2.3])

print("Original:", arr)
print("round:   ", np.round(arr))     # Nearest integer (banker's)
print("floor:   ", np.floor(arr))     # Round toward -∞
print("ceil:    ", np.ceil(arr))      # Round toward +∞
print("trunc:   ", np.trunc(arr))     # Round toward 0

Output:

Original: [ 1.7  2.3 -1.7 -2.3]
round:    [ 2.  2. -2. -2.]
floor:    [ 1.  2. -2. -3.]
ceil:     [ 2.  3. -1. -2.]
trunc:    [ 1.  2. -1. -2.]
Function Behavior 1.7 -1.7
np.round() Nearest integer (half-even) 2 -2
np.floor() Always round down (toward -∞) 1 -2
np.ceil() Always round up (toward +∞) 2 -1
np.trunc() Always round toward zero 1 -1

Common Mistake: Expecting Exact Decimal Representation

Due to how floating-point numbers are stored in binary, some decimal values cannot be represented exactly. This can lead to surprising rounding results:

import numpy as np

value = np.round(2.675, decimals=2)
print(f"np.round(2.675, 2) = {value}")

Output:

np.round(2.675, 2) = 2.68

You might expect 2.68, but 2.675 is stored internally as something slightly less than 2.675, so it rounds down to 2.67.

Summary

Task Code Example Result
Round to integer np.round(arr) [1.7, 2.3] → [2., 2.]
Round to N decimals np.round(arr, decimals=N) [1.534] → [1.53] (N=2)
Round to nearest 10 np.round(arr, decimals=-1) [1234] → [1230]
Round 2D arrays np.round(arr_2d, decimals=N) Works element-wise

To round NumPy array elements, use np.round(array, decimals=N) where N is the desired number of decimal places.

Remember that NumPy uses banker's rounding (round half to even), negative decimals values round to the left of the decimal point, and floating-point representation can occasionally produce unexpected results with values exactly at the halfway point.

Navigation