The round_() function in NumPy rounds the elements of an array to a specified number of decimal places. This function is extremely useful when working with floating-point numbers and when precision is important in scientific computing or data analysis.
Syntax: numpy.round_(arr, decimals=0, out=None)
Parameters:
- arr: array_like – The input array to be rounded.
- decimals: int, optional – The number of decimal places to round to. The default is 0. A negative value will round to the left of the decimal point.
- out: optional – The output array where the result is stored. If not specified, a new array is returned.
The function returns an array with rounded values, having the same type as the input.
Example 1: Rounding Floating-Point Numbers
Let’s see how to use numpy.round_() to round an array of floating-point numbers to the nearest integer (default behavior).
Python
import numpy as np
# Example 1: Rounding values to nearest integer
in_array = [0.5, 1.5, 2.5, 3.5, 4.5, 10.1]
print("Input array:", in_array)
# Round the array
round_off_values = np.round_(in_array)
print("Rounded values:", round_off_values)
OutputInput array: [0.5, 1.5, 2.5, 3.5, 4.5, 10.1]
Rounded values: [ 0. 2. 2. 4. 4. 10.]
In this example, numpy.round_() rounds each element of the array to the nearest integer. The values are rounded to the nearest whole number, with .5 rounded to the nearest even number.
Example 2: Rounding to Specific Decimal Places
You can also round numbers to a specific number of decimal places by using the decimals parameter.
Python
# Example 2: Rounding to 2 decimal places
import numpy as np
in_array = [0.5538, 1.33354, 0.71445]
print("Input array:", in_array)
# Round the array to 3 decimal places
round_off_values = np.round_(in_array, decimals=3)
print("Rounded values:", round_off_values)
OutputInput array: [0.5538, 1.33354, 0.71445]
Rounded values: [0.554 1.334 0.714]
Example 3: Rounding to the Left of the Decimal Point
You can also use negative values in the decimals parameter to round numbers to the left of the decimal point (i.e., round to the nearest tens, hundreds, etc.).
Python
# Example 3: Rounding to the nearest hundred
import numpy as np
in_array = [133, 344, 437, 449, 12]
print("Input array:", in_array)
# Round the array to the nearest hundred
round_off_values = np.round_(in_array, decimals=-2)
print("Rounded values (nearest hundred):", round_off_values)
OutputInput array: [133, 344, 437, 449, 12]
Rounded values (nearest hundred): [100 300 400 400 0]
Example 4: Rounding to the Nearest Thousand
You can also round to the nearest thousand by using a negative value for the decimals parameter.
Python
# Example 4: Rounding to the nearest thousand
import numpy as np
in_array = [133, 344, 437, 449, 12]
print("Input array:", in_array)
# Round the array to the nearest thousand
round_off_values = np.round_(in_array, decimals=-3)
print("Rounded values (nearest thousand):", round_off_values)
OutputInput array: [133, 344, 437, 449, 12]
Rounded values (nearest thousand): [0 0 0 0 0]
Similar Reads
numpy.trunc() in Python The numpy.trunc() is a mathematical function that returns the truncated value of the elements of array. The trunc of the scalar x is the nearest integer i which, closer to zero than x. This simply means that, the fractional part of the signed number x is discarded by this function. Syntax : numpy.tr
2 min read
numpy.rint() in Python The numpy.rint() is a mathematical function that rounds elements of the array to the nearest integer. Syntax : numpy.rint(x[, out]) = ufunc ârintâ) Parameters : array : [array_like] Input array. Return : An array with all array elements being rounded off, having same type and shape as input. Code #1
2 min read
Python | Numpy matrix.round() With the help of Numpy matrix.round() method, we are able to round off the values of the given matrix. Syntax : matrix.round() Return : Return rounded values in matrix Example #1 : In the given example we are able to round off the given matrix by using matrix.round() method. Python3 1=1 # import the
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.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.around() in Python 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.Pythonimport numpy as np a
2 min read