How to round array elements to the given number of decimals using NumPy?


The round() function of the Numpy library is used to round array elements to the given number of decimals. Numpy is a Python library that is used to perform mathematical operations, manipulation, and creation of arrays, etc. In this article, we will use the round() function to round array elements to the given number of decimals.

Method 1: using round() function

Syntax of round() function

numpy.round(arr,decimals=0, out=None)

Here, arr is the input array to be rounded, decimal is the number of decimal places the elements of the array are to be rounded. By default, the decimal value is 0. Out is the output array which is created after rounding the elements of the original array.

Example 1: Rounding a single array element

In the below example, we created an array arr with a single element and then used the round() function to round the array element to 2 decimal places. The resulting rounded array element was then printed.

import numpy as np

# Create an array
arr = np.array([3.14159])

# Round the array element to 2 decimal places
rounded_arr = np.round(arr, decimals=2)

# Print the rounded array element
print(rounded_arr)

Output

[3.14]

Example: Rounding Multiple Array elements

In the below example, we created an array arr with multiple elements and then used the round() function to round the array elements to 1 decimal place. The resulting rounded array elements were then printed.

import numpy as np

# Create an array
arr = np.array([1.234, 2.345, 3.456, 4.567])

# Round the array elements to 1 decimal place
rounded_arr = np.round(arr, decimals=1)

# Print the rounded array elements
print(rounded_arr)

Output

[1.2 2.3 3.5 4.6]

Example 3: Rounding to the nearest integer

In the below example, we created an array arr with multiple elements and then used the round() function to round the array elements to the nearest integer. The resulting rounded array elements were then printed.

import numpy as np

# Create an array
arr = np.array([1.234, 2.345, 3.456, 4.567])

# Define the precision
precision = 0.001

# Calculate the multiplier needed to achieve the desired precision
multiplier = 1 / precision

# Round the array elements to the nearest integer
rounded_arr = np.round(arr * multiplier) / multiplier

# Print the rounded array elements
print(rounded_arr)

Output

1.234 2.345 3.456 4.567]

Example 4: Rounding to a specified precision

In the below example, we first define the desired precision as a variable precision. We then calculate the multiplier needed to achieve the desired precision by dividing 1 by the precision. We multiply the array elements by this multiplier before rounding and then divide them back by the multiplier to obtain the rounded values at the desired precision.

import numpy as np

# Create an array
arr = np.array([1.234, 2.345, 3.456, 4.567])

# Define the precision
precision = 0.001

# Calculate the multiplier needed to achieve the desired precision
multiplier = 1 / precision

# Round the array elements to the nearest integer
rounded_arr = np.round(arr * multiplier) / multiplier

# Print the rounded array elements
print(rounded_arr)

Output

[1.234 2.345 3.456 4.567]

Method 2: Using numpy.floor() method

The numpy.floor method returns the largest integer not greater than the input element. When applied to an array, it rounds down each element to the nearest integer or decimal place toward negative infinity.

Syntax

numpy.floor(arr, decimals)

Here, arr is the Input array, and decimals (optional) are the number of decimal places to round to. The default is 0.

Example

In the below example, the code creates a NumPy array arr containing floating-point values. It then uses the numpy.floor method to round down each element in the array to the nearest 0.1 (i.e., 1 decimal place) towards negative infinity. The resulting rounded array is stored in a variable called rounded_arr. Finally, the rounded array is printed to the console using the print function.

import numpy as np

# Create an array
arr = np.array([1.234, 2.345, 3.456, 4.567])

# Round down the array elements to 1 decimal place
rounded_arr = np.floor(arr * 10) / 10

# Print the rounded array elements
print(rounded_arr)

Output

[1.2 2.3 3.4 4.5]

Method 3: Using numpy.ceil()

The numpy.ceil method returns the smallest integer not less than the input element. When applied to an array, it rounds up each element to the nearest integer or decimal place towards positive infinity.

Syntax

numpy.ceil(arr, decimals)

Here, arr is the Input array, and decimals (optional) are the number of decimal places to round to. The default is 0.

Example

In the below example, the code creates a NumPy array arr containing floating-point values. It then uses the numpy.ceil method to round up each element in the array to the nearest 0.1 (i.e., 1 decimal place) towards positive infinity. The resulting rounded array is stored in a variable called rounded_arr. Finally, the rounded array is printed to the console using the print function.

import numpy as np

# Create an array
arr = np.array([1.234, 2.345, 3.456, 4.567])

# Round up the array elements to 1 decimal place
rounded_arr = np.ceil(arr * 10) / 10

# Print the rounded array elements
print(rounded_arr)

Output

[1.3 2.4 3.5 4.6]

Method 4: Using numpy.trunc() method

The numpy.trunc method returns the integer part of the input element. When applied to an array, it rounds toward zero by dropping the decimal part of each element.

Syntax

numpy.trunc(arr)

Here, arr is the input array that contains the decimal elements that need to be rounded.

Example

In the below example, we create a NumPy array arr containing floating-point values. It then uses the numpy.trunc method to round down each element in the array to the nearest integer by dropping the decimal part of the value. The resulting truncated array is stored in a variable called truncated_arr. Finally, the truncated array is printed to the console using the print function.

import numpy as np

# Create an array
arr = np.array([1.234, 2.345, 3.456, 4.567])

# Truncate the array elements to integers
truncated_arr = np.trunc(arr)

# Print the truncated array elements
print(truncated_arr)

Output

[1. 2. 3. 4.]

Conclusion

In this article, we have discussed how we can round array elements to the given number of decimals using numpy. The Numpy library provides a round() function to perform the rounding operation on array elements. We can round array elements to any desired number of decimals using the round() method. We can also use numpy.floor(),numpy.ceil() and numpy.trunc() to round the numpy array elements.

Updated on: 11-Jul-2023

198 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements