# np.fix: How to Round Array Elements in Python

Numpy fix() is a mathematical function that rounds elements of the array to the nearest integer towards zero. The rounded values are returned as floats.

The np.fix() method contains two parameters, out of which one is optional, and returns the rounded values as float datatype.

The np.fix() method is defined under NumPy, which can be imported as import NumPy as np.

We can create multidimensional arrays and derive other mathematical statistics with the help of NumPy, a Python library.

### Syntax

`numpy.fix(a, b = None)`

### Parameters

The np.fix() method takes two parameters, out of which one is optional. The first parameter is the input array for which we want to find the rounded-off values, and the second is the output array with which the result will be placed.

### Return Value

The fix() function returns an array containing values rounded off to the nearest integer towards zero and returns the value if any scalar value is passed based on the same principles.

### Example

Write a program to show the fix() function’s working in Python.

```# app.py

import numpy as np

a = [1.2, 2.3, 4.5, 6.7]
b = [3, 4.53, 6.9900, 7, 0.28888, 99.3]
print("Input array: ", a, "\n")
print("Output array: ", np.fix(a), "\n")
print("Input array: ", b, "\n")
print("Output array: ", np.fix(b))
```

#### Output

```python3 app.py
Input array:  [1.2, 2.3, 4.5, 6.7]

Output array:  [1. 2. 4. 6.]

Input array:  [3, 4.53, 6.99, 7, 0.28888, 99.3]

Output array:  [ 3.  4.  6.  7.  0. 99.]```

In this example, we’ve seen that by bypassing the values in the array, we are getting elements rounded off to their nearest element towards zero.

#### Write a program to show the use of the fix() function on scalar values.

See the following code.

```# app.py

import numpy as np

a = 3.4
b = 4
c = 5.7
print("a = ", a, " fix() ", np.fix(a), "\n")
print("b = ", a, " fix() ", np.fix(b), "\n")
print("c = ", a, " fix() ", np.fix(c), "\n")```

#### Output

```python3 app.py
a =  3.4  fix()  3.0

b =  3.4  fix()  4.0

c =  3.4  fix()  5.0```

Here, we are getting the desired output for each scalar value.

## Conclusion

Numpy fix() function rounds to the nearest integer towards zero. It rounds an array of floats element-wise to the nearest integer towards zero. The rounded values are returned as floats.

This site uses Akismet to reduce spam. Learn how your comment data is processed.