NumPy rint()

The numpy.rint() function rounds each element in an input array to the nearest integer while maintaining the original data type.

Syntax

</>
Copy
numpy.rint(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)

Parameters

ParameterTypeDescription
xarray_likeInput array containing floating-point numbers.
outndarray, None, or tuple of ndarray and None, optionalOptional output array to store the results. If None, a new array is created.
wherearray_like, optionalBoolean mask that specifies where rounding should be applied.
castingstr, optionalDefines the casting behavior when computing the function.
orderstr, optionalMemory layout order of the output array.
dtypedata-type, optionalSpecifies the data type of the output array.
subokbool, optionalDetermines if subclasses of ndarray are preserved in the output.

Return Value

Returns an array with elements rounded to the nearest integer. If the input is a scalar, a scalar is returned.


Examples

1. Rounding a Single Floating-Point Number

This example demonstrates how numpy.rint() rounds a single number to the nearest integer.

</>
Copy
import numpy as np

# Define a floating-point number
num = 3.6  

# Compute the rounded value
rounded_value = np.rint(num)

# Print the result
print("Rounded value:", rounded_value)

Output:

Rounded value: 4.0