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
| Parameter | Type | Description |
|---|---|---|
x | array_like | Input array containing floating-point numbers. |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array to store the results. If None, a new array is created. |
where | array_like, optional | Boolean mask that specifies where rounding should be applied. |
casting | str, optional | Defines the casting behavior when computing the function. |
order | str, optional | Memory layout order of the output array. |
dtype | data-type, optional | Specifies the data type of the output array. |
subok | bool, optional | Determines 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
