What is math.trunc() in Python?

Python is a high-level programming language that provides functionalities for a number of operations. The math module comes with mathematical methods and constraints, which make for straightforward calculations.

math.trunc() returns the integer part of the value passed as a parameter.

Figure 1 : Visual Representation of math.trunc function

Syntax

math.trunc(x)

Parameters

  • x: The value to be truncated. This parameter is required.

Return value

This function returns the truncated integer part of the value passed.

Code

#import library
import math
id1 = math.trunc(5.463)
print("5.463: ",id1)
id2 = math.trunc(-10.43)
print("-10.43: ",id2)
id3 = math.trunc(0.000)
print("0.000: ",id3)
id4 = math.trunc(293.27)
print("293.27: ",id4)

Free Resources