Numpy provides two identical methods to do this. Either use
np.round(data, 2)
or
np.around(data, 2)
as they are equivalent.
See the documentation for more information.
Examples:
>>> import numpy as np
>>> a = np.array([0.015, 0.235, 0.112])
>>> np.round(a, 2)
array([0.02, 0.24, 0.11])
>>> np.around(a, 2)
array([0.02, 0.24, 0.11])
>>> np.round(a, 1)
array([0. , 0.2, 0.1])
Answer from Joe Iddon on Stack OverflowNote.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Round up/down array elements (np.floor, np.trunc, np.ceil) | note.nkmk.me
January 15, 2024 - You can use np.floor(), np.trunc(), and np.ceil() to round up and down the elements in a NumPy array (ndarray). Considering both positive and negative values, there are four main types of rounding: to ...
Programiz
programiz.com › python-programming › numpy › methods › floor
NumPy floor() (With Examples)
The floor() function returns an array that contains the rounded-down values of each element in the input array. import numpy as np # create a 2-D array array1 = np.array([[1.2, 2.7, 3.5], [4.9, 5.1, 6.8]])
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.round.html
numpy.round — NumPy v2.1 Manual
Number of decimal places to round to (default: 0).
NumPy
numpy.org › doc › stable › reference › generated › numpy.around.html
numpy.around — NumPy v2.4 Manual
Round an array to the given number of decimals.
NumPy
numpy.org › doc › stable › reference › generated › numpy.round.html
numpy.round — NumPy v2.4 Manual
Number of decimal places to round to (default: 0).
Top answer 1 of 4
142
Numpy provides two identical methods to do this. Either use
np.round(data, 2)
or
np.around(data, 2)
as they are equivalent.
See the documentation for more information.
Examples:
>>> import numpy as np
>>> a = np.array([0.015, 0.235, 0.112])
>>> np.round(a, 2)
array([0.02, 0.24, 0.11])
>>> np.around(a, 2)
array([0.02, 0.24, 0.11])
>>> np.round(a, 1)
array([0. , 0.2, 0.1])
2 of 4
10
It is worth noting that the accepted answer will round small floats down to zero as demonstrated below:
>>> import numpy as np
>>> arr = np.asarray([2.92290007e+00, -1.57376965e-03, 4.82011728e-08, 1.92896977e-12])
>>> print(arr)
[ 2.92290007e+00 -1.57376965e-03 4.82011728e-08 1.92896977e-12]
>>> np.round(arr, 2)
array([ 2.92, -0. , 0. , 0. ])
You can use set_printoptions and a custom formatter to fix this and get a more numpy-esque printout with fewer decimal places:
>>> np.set_printoptions(formatter={'float': "{0:0.2e}".format})
>>> print(arr)
[2.92e+00 -1.57e-03 4.82e-08 1.93e-12]
This way, you get the full versatility of format and maintain the precision of numpy's datatypes.
Also note that this only affects printing, not the actual precision of the stored values used for computation.
Medium
medium.com › @heyamit10 › understanding-numpy-round-up-ceil-vs-round-vs-floor-f155922395b8
Understanding NumPy Round Up (ceil vs round vs floor) | by Hey Amit | Medium
April 12, 2025 - np.round(): Rounds to the nearest integer (up or down based on standard rounding rules). You might be wondering: “Why does this matter?” Well, in practical scenarios — like calculating prices, dealing with coordinates, or ensuring buffer limits — you often need to force numbers up instead of relying on regular rounding. Let’s see how each of these functions works in action: import numpy as np arr = np.array([1.2, 2.5, 3.8, -4.3, -5.9]) # Rounding up (ceil) rounded_up = np.ceil(arr) # Regular rounding rounded_normal = np.round(arr) # Rounding down (floor) rounded_down = np.floor(arr) print("Original:", arr) print("Ceil (Round Up):", rounded_up) # [ 2.
Codecademy
codecademy.com › docs › python:numpy › math methods › .floor()
Python:NumPy | Math Methods | .floor() | Codecademy
May 14, 2025 - In the NumPy library, the .floor() method rounds down a number or an array of numbers to the nearest integer that is less than or equal to the given value. It returns an array, with the elements separated by commas.
Real Python
realpython.com › python-rounding
How to Round Numbers in Python – Real Python
December 7, 2024 - The way in which computers store floating-point numbers in memory naturally introduces a subtle rounding error, but you learned how to work around this with the decimal module in Python’s standard library. You can round NumPy arrays and pandas Series and DataFrame objects. There are best practices for rounding with real-world data. Get Your Code: Click here to download the free sample code you’ll use to learn about rounding numbers in Python.
Python Guides
pythonguides.com › python-numpy-round
Np.round() Function In Python
May 19, 2025 - Learn how to use NumPy's np.round() function to round decimal values in Python arrays with precision and control. Perfect for data science and numerical computations.
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Round array elements (np.round, np.around, np.rint) | note.nkmk.me
January 15, 2024 - After adjusting the number to the desired decimal place, 0.5 is added. Then, np.floor() is applied to truncate the decimal places, effectively rounding down towards negative infinity. Finally, the number is readjusted to its original decimal place.
NumPy
numpy.org › devdocs › reference › generated › numpy.round.html
numpy.round — NumPy v2.5.dev0 Manual
Number of decimal places to round to (default: 0).
NumPy
numpy.org › devdocs › reference › generated › numpy.rint.html
numpy.rint — NumPy v2.5.dev0 Manual
Thus 1.5 and 2.5 round to 2.0, -0.5 and 0.5 round to 0.0, etc.
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.round.html
numpy.round — NumPy v2.2 Manual
Number of decimal places to round to (default: 0).
Medium
medium.com › @heyamit10 › how-to-round-numbers-to-integers-in-numpy-c43b43970c4c
How to Round Numbers to Integers in NumPy? | by Hey Amit | Medium
March 6, 2025 - Well, imagine you’re dealing with financial data, scientific measurements, or any situation where precision matters. Instead of rounding 1.746 to 2, maybe you just want to round it to one decimal place, like 1.7. ... import numpy as np arr = np.array([1.746, 2.312, 3.876, 4.543]) rounded_arr = np.round(arr, decimals=1) print(rounded_arr)
NumPy
numpy.org › doc › stable › reference › generated › numpy.rint.html
numpy.rint — NumPy v2.4 Manual
For values exactly halfway between rounded decimal values, NumPy rounds to the nearest even value.