What is the around function in numpy?

The around 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 around function, the around library must be imported at the program’s start.

import numpy

Syntax

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

Parameters

The around function takes in the following parameters:

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

Return value

The around function returns an array with the same dimension as that of ``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 3 and subsequently 4 decimal numbers.

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

import numpy
print(numpy.around([2.51231, 6.12351, -6123.123123], 3))
print(numpy.around([2.51231, 6.12351, -6123.123123], 4))
Copyright ©2024 Educative, Inc. All rights reserved