Home »
Python
math.trunc() method with example in Python
Python math.trunc() method: Here, we are going to learn about the math.trunc() method with example in Python.
Submitted by IncludeHelp, on April 17, 2019
Python math.trunc() method
math.trunc() method is a library method of math module, it is used to get the truncated integer value of a number, it accepts a number (either an integer or a float) and returns the real value truncated to an integral.
Note: If anything is passed except the number, the method returns a type error, if we pass a string – it will return "TypeError: type str doesn't define __trunc__ method"
Syntax of math.trunc() method:
math.trunc(n)
Parameter(s): n – an integer or a float number.
Return value: int – it returns an integer value that is the integral part of the number n.
Example:
Input:
a = 10.23
# function call
print(math.trunc(a))
Output:
10
Python code to demonstrate example of math.trunc() method
# Python code demonstrate example of
# math.trunc() method
# importing math module
import math
# numbers
a = 10
b = 10.23
c = -10
d = -10.67
e = 10.67
# printing the fractional and integer part
# of the numbers by using math.trunc()
print("trunc(a): ", math.trunc(a))
print("trunc(b): ", math.trunc(b))
print("trunc(c): ", math.trunc(c))
print("trunc(d): ", math.trunc(d))
print("trunc(e): ", math.trunc(e))
Output
trunc(a): 10
trunc(b): 10
trunc(c): -10
trunc(d): -10
trunc(e): 10