NumPy round()

The numpy.round() function rounds elements of an array to a specified number of decimal places. It supports rounding both floating-point and complex numbers.

Syntax

</>
Copy
numpy.round(a, decimals=0, out=None)

Parameters

ParameterTypeDescription
aarray_likeInput array containing values to be rounded.
decimalsint, optionalNumber of decimal places to round to (default: 0). A negative value rounds to the left of the decimal point.
outndarray, optionalOptional output array to store the result. Must match the shape of the expected output.

Return Value

Returns an array with rounded values of the same type as the input. If out is provided, the result is stored in it; otherwise, a new array is created.


Examples

1. Rounding Floating-Point Numbers

Rounding an array of floating-point numbers to 2 decimal places.

</>
Copy
import numpy as np

# Define an array of floating-point numbers
arr = np.array([1.234, 2.678, 3.987])

# Round each element to 2 decimal places
rounded_arr = np.round(arr, decimals=2)

# Print the results
print("Original array:", arr)
print("Rounded array:", rounded_arr)

Output:

Original array: [1.234 2.678 3.987]
Rounded array: [1.23 2.68 3.99]

2. Rounding to the Nearest Integer

By default, numpy.round() rounds numbers to the nearest integer.

</>
Copy
import numpy as np

# Define an array with fractional values
arr = np.array([1.5, 2.3, 3.8, 4.2])

# Round to the nearest integer (default decimals=0)
rounded_arr = np.round(arr)

# Print the results
print("Original array:", arr)
print("Rounded array:", rounded_arr)

Output:

Original array: [1.5 2.3 3.8 4.2]
Rounded array: [2. 2. 4. 4.]

3. Rounding to a Negative Decimal Place

Using a negative decimals value rounds numbers to positions to the left of the decimal point.

</>
Copy
import numpy as np

# Define an array with larger values
arr = np.array([1234, 5678, 91011])

# Round to the nearest hundred (-2 decimal places)
rounded_arr = np.round(arr, decimals=-2)

# Print the results
print("Original array:", arr)
print("Rounded array:", rounded_arr)

Output:

Original array: [  1234   5678  91011]
Rounded array: [  1200   5700  91000]

4. Using the out Parameter

Using an output array to store the results instead of creating a new array.

</>
Copy
import numpy as np

# Define an array of floating-point numbers
arr = np.array([2.345, 5.678, 8.912])

# Create an output array with the same shape
output_array = np.empty_like(arr)

# Round and store the result in output_array
np.round(arr, decimals=1, out=output_array)

# Print the results
print("Computed rounded values:", output_array)

Output:

Computed rounded values: [2.3 5.7 8.9]

5. Rounding Complex Numbers

For complex numbers, both real and imaginary parts are rounded separately.

</>
Copy
import numpy as np

# Define an array of complex numbers
arr = np.array([1.23 + 4.56j, 3.78 + 9.87j])

# Round each component to 1 decimal place
rounded_arr = np.round(arr, decimals=1)

# Print the results
print("Original array:", arr)
print("Rounded array:", rounded_arr)

Output:

Original array: [1.23+4.56j 3.78+9.87j]
Rounded array: [1.2+4.6j 3.8+9.9j]

The real and imaginary parts are rounded separately, preserving the overall complex structure.