Master NumPy rounding functions with this comprehensive tutorial covering np.round(), np.floor(), np.ceil(), np.trunc(), np.rint(), and np.fix() for various rounding strategies.

How to Round Arrays to Nearest Integer Using np.round()
import numpy as np
my_array = np.array([1.66, 2.44, 3.378, -5.43, 6.511113, -7.653])
print(f'The original array: \n {my_array}')
rounded_array = np.round(my_array)
print(f'The rounded array: \n {rounded_array}')
The np.round() function rounds array elements to the nearest integer by default, or to a specified number of decimal places.
How to Remove Decimals and Truncate Arrays Using np.trunc()
The np.trunc() function removes all decimal places without rounding, truncating toward zero (1.9→1, -1.9→-1).
import numpy as np
my_array = np.array([1.66, 2.44, 3.378, -5.43, 6.511113, -7.653])
print(f'The original array: \n {my_array}')
trunc_round_array = np.trunc(my_array)
print(f'The trunc rounded array: \n {trunc_round_array}')
How to Round Down Arrays Using np.floor() Function
The np.floor() function rounds down to the nearest integer, always going toward negative infinity (1.1→1, -1.1→-2).
import numpy as np
my_array = np.array([1.66, 2.44, 3.378, -5.43, 6.511113, -7.653])
print(f'The original array: \n {my_array}')
floor_round_array = np.floor(my_array)
print(f'The floor rounded array: \n {floor_round_array}')
How to Round Up Arrays Using np.ceil() Function
The np.ceil() function rounds up to the nearest integer, always going toward positive infinity (1.1→2, -1.1→-1).
import numpy as np
my_array = np.array([1.66, 2.44, 3.378, -5.43, 6.511113, -7.653])
print(f'The original array: \n {my_array}')
ceil_round_array = np.ceil(my_array)
print(f'The ceil rounded array: \n {ceil_round_array}')
How to Round to Specific Decimal Places Using np.round() with decimals Parameter
You need define decimals parameter of round function. By default decimals = 0.
import numpy as np
my_array = np.array([1.66, 2.44, 3.378, -5.43, 6.511113, -7.653])
print(f'The original array: \n {my_array}')
decimal_round_array = np.round(my_array, decimals=2)
print(f'The rounded array with 2 decimal placess: \n {decimal_round_array}')
How to Round to Nearest Integer Using np.rint() Function
The np.rint() function rounds array elements to the nearest integer using banker’s rounding (round-to-even), matching Python’s round() behavior.
import numpy as np
my_array = np.array([1.66, 2.44, 3.378, -5.43, 6.511113, -7.653])
print(f'The original array: \n {my_array}')
rint_round_array = np.rint(my_array)
print(f'The rounded array to the nearest integer: \n {rint_round_array}')
How to Round Toward Zero Using np.fix() Function
The np.fix() function rounds toward zero (equivalent to truncation for positive numbers, but differs from floor for negative numbers).
import numpy as np
my_array = np.array([1.66, 2.44, 3.378, -5.43, 6.511113, -7.653])
print(f'The original array: \n {my_array}')
fix_round_array = np.fix(my_array)
print(f'The rounded array to the nearest integer towards zero: \n {fix_round_array}')
