The numpy.trunc() function of Python is used to return the truncated value of the input array elements.
- It is important to note that the truncated value t of input value x should be the nearest integer which is closer to zero rather than to x
- With the help of this function, the fractional part of any signed number is discarded.
Syntax of numpy.trunc():
Below we have the required syntax to use this function:
numpy.trunc(array)
Note: In the above syntax, the array is used to indicate the array elements whose truncated values are to be returned.
Returned Values:
This method mainly returns an array containing the truncated values.
Now it’s time to take a look at the examples.
Example 1:
Let’s start with a basic example.
import numpy as np
x = [1.2,-0.344,5.89]
y = np.trunc(x)
print(y)
Output:
[ 1. -0. 5.]
Example 2:
Below we have the code snippet let us take a look at that:
import numpy as np
inp = [123.22,23.4,0.89]
print("The Input array is :")
print(inp)
x = np.trunc(inp)
print("The Output array is :")
print(x)
Output:
The Input array is : [123.22, 23.4, 0.89] The Output array is : [123. 23. 0.]
