NumPy Tutorial NumPy Statistics NumPy References

NumPy - trunc() function

The NumPy trunc() function returns the truncated value of the input, element-wise. The truncated value of the scalar x is the nearest integer i which is closer to zero than x is.


numpy.trunc(x, out=None)


x Required. Specify array_like as input array.
out Optional. Specify a location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.

Return Value

Returns an array containing the truncated values if out=None. If an output array is specified, a reference to out is returned.


The example below shows the usage of trunc() function.

import numpy as np
Arr = np.array([[3.4,5.4],[-7.2,-15.4]])

print("Arr is:")
print(Arr )

#truncate the value (rounding to 
#nearest integer towards zero) 
print("\nTruncated Array is:")

The output of the above code will be:

Arr is:
[[  3.4   5.4]
 [ -7.2 -15.4]]

Truncated Array is:
[[  3.   5.]
 [ -7. -15.]]

❮ NumPy - Functions