Python NumPy round()
is a built-in function used to return the rounded values of the source array to the nearest integer. It also takes the decimal values to be rounded. If the decimal places to be rounded are specified then it returns an array that contains a float number that will be rounded to the decimal places provided as input. If the decimal places to be rounded are not specified, it is considered as 0, and it will round to the nearest integer.
In this article, I will explain the Python NumPy round()
array function syntax, parameters, and usage of how to find the round values of elements of an input array with examples.
1. Quick Examples of NumPy Array round() Function
Following are the quick examples of the NumPy array round() function.
# Quick examples of numpy array round() function
# Example 1: Get the rounding values of an elements
arr=np.array([1.57279, 2.0843, 2.795,3.1465])
arr1 = np.round(arr)
# Example 2: Get the rounding values of
# Specified decimals is 2
arr=np.array([1.57279, 2.0843, 2.795,3.1465])
arr1 = np.round(arr,decimals = 2)
# Example 3: Get the rounding values of
# Specified decimals is 3
arr=np.array([1.57279, 2.0843, 2.795,3.1465])
arr1 = np.round(arr,decimals = 3)
# Example 4: Get the rounding values of
# Specified decimals is -1
arr=np.array([157279,456, 723, 20843, 2795, 31465])
arr1 = np.round(arr,decimals = -1)
# Example 5: Get the rounding values of
# Specified decimals is -2
arr=np.array([157279,456, 723, 20843, 2795, 31465])
arr1 = np.round(arr,decimals = -2)
# Example 6: Get the rounding values of
# Specified decimals is -3
arr=np.array([157279,456, 723, 20843, 2795, 31465])
arr1 = np.round(arr,decimals = -3)
2. Syntax of NumPy Array round()
Following is the syntax of the Python NumPy round() function.
# Syntax of NumPy round()
numpy.round(array, decimals = 0)
2.1 Parameters of round()
This function allows mainly two parameters.
array
: It is an input array in which we wanted to perform the round-off function.decimals
: The number of decimals to be rounded. It is optional, and if it is not specified, it defaults to 0, it will round to the nearest integer, and the return type will also be an integer.
2.2 Return value
It returns an integer value if the decimal place is not provided and returns a float value if the decimal place is provided.
3. Usage of NumPy Array round()
The round()
function is a mathematical function that returns an array with rounded values of the input array. when you provide an input array with float values that you want to round the digits to our desired number of decimal places. In the NumPy round()
function no need to specify a decimal places argument, by default, it rounds to the nearest integer, if you specify the decimal places it will round the specified decimal places.
Note
: The value rounds to +1 if the value after the decimal point is >=5 else it returns the value as it is up to the decimal places specified.
Let’s discuss a basic example for understanding how the NumPy round function works. In order to find the rounded values of the given array first, you have to create NumPy array using numpy.array()
function.
# Import numpyn
import numpy as np
# Get the rounding values of an elements
arr = np.array([1.57279, 2.0843, 2.795,3.1465])
arr1 = np.round(arr)
print("After getting the rounded values of elements:\n",arr1)
Yields below output.
4. Get the Rounded Values of Specified Decimals
If you want to get the rounded values of specific decimals for a given array, you can use the numpy.round()
function. For instance, the numpy.round()
function is used to round the elements of the arr
array to 2 decimal places.
By adjusting the value of the decimals
variable, you can round the array elements to the specific number of decimal places you need.
# Get the rounding values of
# Specified decimals is 2
arr = np.array([1.57279, 2.0843, 2.795,3.1465])
arr1 = np.round(arr,decimals = 2)
print("After getting the rounded values of specified decimals:\n",arr1)
Yields below output.
You can also specify a specific number of decimal places when using the numpy.round()
function in NumPy. To round an array to three decimal places, you can set the decimals
parameter to 3.
In the below example, the numpy.round()
function is used to round the elements of the arr
array to three decimal places. By specifying decimals=3
, you round the array to three decimal places as desired. You can adjust the decimals
parameter to round to any specific number of decimal places you need.
# Get the rounding values of
# Specified decimals is 3
arr = np.array([1.57279, 2.0843, 2.795,3.1465])
arr1 = np.round(arr,decimals = 3)
print("After getting the rounded values of specified decimals:\n",arr1)
# Output :
# After getting the rounded values of specified decimals:
# [1.573 2.084 2.795 3.146]
5. Get the Rounded Values of Nearest 10’s
If you want to round the values of an array to the nearest multiple of 10, you can use the numpy.round()
function. In this program, the numpy.round()
function is used with decimals=-1
to round the elements of the arr
array to the nearest multiple of 10. Each element in the array has been rounded to the nearest multiple of 10. For example, 157279 is rounded up to 157280, 456 is rounded up to 460, and so on.
# Get the rounding values of
# Specified decimals is -1
arr = np.array([157279,456, 723, 20843, 2795, 31465])
arr1 = np.round(arr,decimals = -1)
print("After getting the rounded values of the nearest 10s:\n",arr1)
# Output:
# After getting the rounded values of the nearest 10s:
# [157280 460 720 20840 2800 31460]
6. Get the Rounded Values of Nearest 100’s
You can get rounded the values of an array to the nearest multiple of 100, you can use the numpy.round()
function. In this code, the numpy.round()
function is used with decimals=-2
to round the elements of the arr
array to the nearest multiple of 100. Each element in the array has been rounded to the nearest multiple of 100. For example, 157279 is rounded up to 157300, 456 is rounded up to 500, and so on.
# Get the rounding values of
# Specified decimals is -2
arr = np.array([157279,456, 723, 20843, 2795, 31465])
arr1 = np.round(arr,decimals = -2)
print("After getting the rounded values of the nearest 100's:\n",arr1)
# Output:
# After getting the rounded values of the nearest 100's:
# [157300 500 700 20800 2800 31500]
7. Get the Rounded Values of Nearest 1000’s
You can also round the values of an array to the nearest multiple of 1000, you can use the numpy.round()
function. In this program, the numpy.round()
function is used with decimals=-3
to round the elements of the arr
array to the nearest multiple of 1000. Each element in the array has been rounded to the nearest multiple of 1000. For instance, 157279 is rounded down to 157000, 456 is rounded to 0 (because it’s less than 500), 723 is rounded up to 1000, and so on.
# Get the rounding values of
# Specified decimals is -3
arr = np.array([157279,456, 723, 20843, 2795, 31465])
arr1 = np.round(arr,decimals = -3)
print("After getting the rounded values of the nearest 1000's:\n",arr1)
# Output:
# After getting the rounded values of the nearest 1000's:
# [157000 0 1000 21000 3000 31000]
Frequently Asked Questions
The numpy.round()
function in NumPy is used to round the elements of an array to the nearest integer or to a specified number of decimals. It follows the standard rounding rules: if the fractional part of the number is greater than or equal to 0.5, the number is rounded up to the nearest integer; otherwise, it is rounded down.
To round an array to a specific number of decimal places using the numpy.round()
function, you can pass the desired number of decimal places as the decimals
parameter.
You can use numpy.round()
to round a scalar value to the nearest integer or to a specific number of decimal places.
You can round an array to the nearest integer by simply calling numpy.round()
without specifying the decimals
parameter. It will round the elements to the nearest integer.
The numpy.round()
always rounds to the nearest integer. If you need to round towards positive infinity, you can use numpy.ceil(), and for rounding towards negative infinity, you can use numpy.floor()
.
Conclusion
In this article, I have explained the Numpy round()
function using various examples of how to round elements in the NumPy array. I have also explained how to round the values using different decimal places.
Happy learning !!
Related Articles
- NumPy array operations
- NumPy Norm of Vector
- Python NumPy Square Root
- Get the ceil values of an array
- How to get floor value of an array?
- How to get square value of an array?
- NumPy Element Wise Multiplication
- Python NumPy Array mean() Function
- How to get Diagonal of NumPy Array Using diag()