numpy.round_() in Python (original) (raw)
Last Updated : 06 Dec, 2024
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)
`
Output
Input 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)
`
Output
Input 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)
`
Output
Input 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)
`
Output
Input array: [133, 344, 437, 449, 12] Rounded values (nearest thousand): [0 0 0 0 0]