Python:NumPy round()

Anonymous contributor's avatar
Anonymous contributor
Published Oct 29, 2025

The round() method in NumPy rounds each element of an array to the nearest integer or to a specified number of decimal places. Values exactly halfway between two numbers are rounded to the nearest even value (also known as banker’s rounding).

  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 Courses
    • With Professional Certification
    • Beginner Friendly.
      95 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

ndarray.round(decimals=0, out=None)

Parameters:

  • decimals (int, optional): Number of decimal places to round to. Default is 0. Can be negative to round to the left of the decimal point.
  • out (ndarray, optional): An alternative output array where results are stored. Must be the same shape as the input.

Return value:

Returns an array with elements rounded to the specified number of decimals. If out is provided, the result is stored in that array, and a reference to it is returned.

Example 1: Round to the nearest whole number

This example rounds all elements in a NumPy array to the nearest integer:

import numpy as np
arr = np.array([1.34566, 4.55, 7.788, 2.09, 9.45])
rounded_arr = arr.round()
print(rounded_arr)

This produces the following output:

[1. 5. 8. 2. 9.]

Example 2: Round to specific decimal places

This example rounds all elements in a NumPy array to two decimal places:

import numpy as np
ndarray = np.array([1.34566, 4.55, 7.788, 2.09, 9.45])
two_decimal_places = np.round(ndarray, decimals=2)
print(two_decimal_places)

This produces the following output:

[1.35 4.55 7.79 2.09 9.45]

Codebyte Example

This example shows how to round NumPy array elements to various decimal places using round():

Code
Output

All contributors

Learn Python:NumPy on Codecademy

  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 Courses
    • With Professional Certification
    • Beginner Friendly.
      95 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours