NumPy fix()

The numpy.fix() function rounds each element in an input array to the nearest integer towards zero.

Syntax

</>
Copy
numpy.fix(x, out=None)

Parameters

ParameterTypeDescription
xarray_likeAn array of floating-point values to be rounded towards zero.
outndarray, optionalOptional output array to store the rounded values. If not provided, a new array is created.

Return Value

Returns an array with the same shape as the input, with values rounded towards zero. The returned array retains the same data type as the input.


Examples

1. Rounding a Single Floating-Point Number

Here, we round a single floating-point number towards zero.

</>
Copy
import numpy as np

# Define a single floating-point number
num = -2.7

# Apply numpy.fix()
result = np.fix(num)

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

Output:

Rounded value: -2.0