Python NumPy round() Array Function

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 how to find the round values of elements of an input array, using the numpy.round() function with example.

1. Quick Examples of Python NumPy Array round() Function

Following are the quick examples of Python NumPy round().


# Below are the quick examples

# 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 and returns an array with rounded values of the input array. when we provide an input array with float values that we wanted 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 we 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, we have to create NumPy array using numpy.array() function.


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(arr1)

# Output :
# [2. 2. 3. 3.]

4. Get the Rounded Values of Specified Decimals

We can add decimals = 2 as a parameter of numpy.round() to specify the number of places we want the round the elements of an input array.


# 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(arr1)

# Output :
# [1.57 2.08 2.8  3.15]

We can also specify some code for three decimal places.


# 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(arr1)

# Output : 
# [1.573 2.084 2.795 3.146]

5. Get the Rounded Values of Nearest 10’s

Using negative decimal places we can round the nearest tens.


# 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(arr1)

# Output :
# [157280    460    720  20840   2800  31460]

6. Get the Rounded Values of Nearest 100’s

By using negative decimal places we can round the nearest hundred.


# 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(arr1)

# Output :
# [157300    500    700  20800   2800  31500]

7. Get the Rounded Values of Nearest 1000’s

We can also find out the round value of the nearest 1000’s by using negative decimals.


# 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(arr1)

# Output : 
# [157000      0   1000  21000   3000  31000]

8. 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 !!

References

Vijetha

With 5 of experience in technical writing, I have had the privilege to work with a diverse range of technologies like Python, Pandas, NumPy and R. During this time, I have consistently demonstrated my ability to grasp intricate technical details and transform them into comprehensible materials.

Leave a Reply

You are currently viewing Python NumPy round() Array Function