# Numpy trunc() – Return the truncated value of the input, element-wise

Today, we’ll learn how to get the truncated value of inputs on our computer using the trunc() function. This function is similar to the ceiling and floor functions, but with a slightly different domain and range. To use this function, we need to import the NumPy module. Once we have imported NumPy, we can use the trunc() function to get the truncated value of any input.

Let us understand our `trunc` concept theoretically before getting implemented on our computers today.

## What is Numpy.trunc() method?

The truncated value of the scalar x is the nearest integer y which is closer to zero than x is. Please have a look at the two examples below.

• The truncated value of -2.35 is -2.0. The nearest integers of -2.35 are -3 and -2. -2 is closer to 0 than -3. So the truncated value for -2.35 is -2.0.
• The truncated value of 3.35 is 3. The nearest integers of 3.35 are 3 and 4. 3 is closer to 0 than 4. So the truncated value for 3.35 is 3.
• The truncated value for 0 is 0, and for -3 is -3, and for 5 is 5. (Truncated value for integers is same as the number)

## Using Numpy.trun()

Now we will see how to implement this function in our code snippet today. Let us see the syntax below.

```numpy.trunc(x, out=None, where=True, casting='same_kind', order='K', subok : [bool, datatype])
```

The parameters used in the above snippet function as follows:

• x: It can be a variable containing a value in Radian or it may be an array containing some value
• out: A location in which the result is to be stored. If provided, it must have a shape similar to the input X. If not provided or None, a fresh value or result is returned. It’s optional.
• where = true:  Where the condition is True, We must get our result or output for “where = False” We will not get any result. It’s optional. y default its value is True.
• casting=’same_kind’: It means only float64 to float32 value or results are allowed. The function shout cast values in this range or datatype.
• order = ‘K’: It always results in an output array in the form of k ordered. (Note: There are 4 types of orders as {‘K’, ‘C’, ‘F’, ‘A’}). It’s optional.
• subok: [bool, datatype] to make a subclass of the result or not. if True then provide a name for the subclass. It returns an array with the same shape and type as a given array. It’s also optional.

Now we will implement this function in our examples below. We should try two examples as well. And last, we will see the graphical representation for the same.

## Example 1 – Working with two values

In this example, we passed two values and received our output. In the second implementation, we passed a parameter for the out parameter. By default, the out parameter formats the output array. So, the truncated value for 3.25 was loaded into the c array and printed the same.

```import numpy as np
b = np.trunc(-2.35)
print(b)

np.trunc(3.25, c)
print(c)

#output
-2.0
[3. 3. 3. 3. 3. 3. 3.]
```

## Example 2 – Numpy.trunc() on an array of values

In this example, we are passing an array of values in our function. It will return a resultant array comprising of all corresponding truncated values for all values of the input array.

```import numpy as np
input = np.array([-2.35, -1,  -0.36, 0, 0.36, 1, 5.69])
d=np.trunc(input)
print(d)

#output
[-2. -1. -0.  0.  0.  1.  5.]

```

## Plotting numpy.trunc() on a graph

The numpy.trunc() function is used to return the truncated value of the input element, i.e. the integer part of the input element. If we have an input array, this function returns the truncated value for each element in the array. We can also represent the truncated value of a single element in a graphical manner using the x-y graph.

```import matplotlib.pyplot as plt
import numpy
x=[]
y=[]
i=-6.99
while (i<7.00):
x.append(i)
y.append(numpy.trunc(i))
i=i+0.01

plt.xlabel("x axis")
plt.ylabel("y axis")
plt.grid(linestyle='-', linewidth=0.5,color='red')
plt.plot(x,y, linewidth=3, color = 'black')
```

The above code snippet will give the output as below.

## Conclusion

So today, We covered the Numpy truncated function implementation in our code using python. We saw the graphical representation of this function for some limits. from this graph, You can somewhat analyze the function and how it works. Through this, You can much better understand our topics. We must visit again with some more exciting topics.