Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

NumPy trunc() function

In this tutorial, we will cover numpy.trunc() function in the Numpy Library of Python.

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)


[ 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)


The Input array is :
[123.22, 23.4, 0.89]
The Output array is :
[123. 23. 0.]

Summary:

This tutorial was all about numpy.trunc() mathematical function in the Numpy library. We had also covered its syntax, parameters as well as the value returned by this function.