The numpy.around() returns a new array with each element rounded to the given number of decimals. It is similar to numpy.round() function and supports various rounding options including rounding to integers or decimal places or even rounding to tens, hundreds and so forth.
Python
import numpy as np
a = np.array([1.2, 2.5, 3.7, 4.4])
res = np.around(a)
print(res)
np.around(a) rounds each element in the array to the nearest integer because the decimals parameter is not specified and defaults to 0. This means the function rounds all numbers to the nearest integer.
Syntax
numpy.around(a, decimals=0, out=None)
Parameters:
- a (array_like): Input array containing numeric values to be rounded.
- decimals (optional int, default=0): Number of decimal places to round to 0 rounds to nearest integer, positive values round to specified decimal places, negative values round left of the decimal like tens.
- out (ndarray, optional): Array to store the result and it must have the same shape as the output.
Returns: This function returns a rounded array with the same shape as the input.
Examples
Lets see some examples widely used:
1. Rounding to 2 decimal places
Python
import numpy as np
a = np.array([1.2345, 2.6789, 3.14159])
res = np.around(a, decimals=2)
print(res)
np.around(a, decimals=2) rounds each element in the array to 2 decimal places. The decimals=2 parameter tells the function to keep two digits after the decimal point for every number.
2. Rounding to the nearest ten (Negative decimals)
Python
import numpy as np
a = np.array([123, 456, 789])
res = np.around(a, decimals=-1)
print(res)
np.around(a, decimals=-1) rounds each element to the nearest ten. Negative decimals round to the left of the decimal point, so -1 rounds to the nearest multiple of 10.
3. Rounding to the nearest hundred
Python
import numpy as np
a = np.array([123, 456, 789])
res = np.around(a, decimals=-2)
print(res)
np.around(a, decimals=-2) rounds each element to the nearest hundred. Negative decimals round to the left of the decimal point, so -2 rounds to the nearest multiple of 100.
4. Using the out parameter to store result in pre-allocated array
Python
import numpy as np
a = np.array([1.567, 2.345, 3.789])
out_array = np.empty_like(a)
np.around(a, decimals=1, out=out_array)
print(out_array)
np.around(...) rounds each element to 1 decimal place and stores the result in out_array, avoiding the creation of a new array.
Similar Reads
numpy.floor() in Python The numpy.floor() function returns the largest integer less than or equal to each element in the input array. It effectively rounds numbers down to the nearest whole number. Let's understand with an example:Pythonimport numpy as np a = [0.5, 1.5, 2.5, 3, 4.5, 10.1] res = np.floor(a) print("Floored:"
1 min read
numpy.fix() in Python The numpy.fix() is a mathematical function that rounds elements of the array to the nearest integer towards zero. The rounded values are returned as floats. Syntax : numpy.fix(a, b = None) Parameters : a : [array_like] Input array to be floated. b : [ndarray, optional] Output array. Return : The arr
2 min read
numpy.argwhere() in Python numpy.argwhere() function is used to find the indices of array elements that are non-zero, grouped by element. Syntax : numpy.argwhere(arr) Parameters : arr : [array_like] Input array. Return : [ndarray] Indices of elements that are non-zero. Indices are grouped by element. Code #1 : Python3 # Pytho
1 min read
numpy.byte_bounds() function â Python numpy.byte_bounds() function returns pointers to the end-points of an array. Syntax : numpy.byte_bounds(arr) Parameters : arr : [ndarray] Input array. Return : [tuple of 2 integers] The first integer is the first byte of the array, the second integer is just past the last byte of the array. If arr i
1 min read
numpy.who function - Python numpy.who() function print the NumPy arrays in the given dictionary. Syntax : numpy.who(vardict = None) Parameters : vardict : [dict, optional] A dictionary possibly containing ndarrays. Return : Returns âNoneâ. If there is no dictionary passed in or vardict is None then returns NumPy arrays in the
1 min read