What is the round_ function in NumPy?

The round_ function is used to round an array of floats to a given number of decimals. It comes with NumPy, which is a widely used library of the high-level programming language Python.

To use the round_ function, the round_ library must be imported at the start of the program.

import numpy

Syntax

numpy.round_(a, decimals=0, out=None)

Paramaters

The round_ function takes in the following parameters:

  • a - an array of floats on which the round_ function will be applied.
  • decimals - (optional) the number of decimals to which the values in a will be rounded. By default, its value is zero.
  • out - (optional) the location where the return value of the round_ function will be stored.

Return value

The round_ function returns an array with the same dimension as that of a and contains rounded values of type float.

If a contains complex numbers, the real and imaginary parts are rounded separately.

Example

The following example demonstrates how to round an array of floats up to three, and subsequently four, decimal numbers.

The return value is printed using the built-in print function, which comes with Python.

import numpy
print(numpy.round_([2.51231, 6.12351, -6123.123123], 3))
print(numpy.round_([2.51231, 6.12351, -6123.123123], 4))

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved