Introduction Python trunc() Method with Examples

Python trunc() method or truncate function is one of the Python Math functions used to remove the decimal values from particular expression and return the integer value. This Function is under the python math library, so we have to import math when we want to use it.

Python trunc()

Syntax


    math.trunc(n)


Parameter(s):

  • n – an integer or a float number.

Return value:

This function returns an integer positive or negative depending upon the given input. But it always tries to round up or down towards zero.

Examples of Python trunc() method

Example 1


# 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

Example 2

Python trunc() with a negative value


# Program to show working of trunc
# Using positive numbers
import math

# Taking imput from user
n = float(input("Enter a negative number: "))
# Showing working of floor
print("Floor is: ", math.floor(n))
# Showing working of ceil
print("Ceil is: ", math.ceil(n))
# Showing working of trunc
print("Trunc is: ", math.trunc(n))

Output


# Program to show working of trunc
# Using positive numbers
import math

# Taking imput from user
n = float(input("Enter a negative number: "))
# Showing working of floor
print("Floor is: ", math.floor(n))
# Showing working of ceil
print("Ceil is: ", math.ceil(n))
# Showing working of trunc
print("Trunc is: ", math.trunc(n))

Example 3

Python trunc() with Tuple and List value


import math

Tup = (21.98, 19.26, 11.05, -40.95 , 50.85) # Tuple Declaration
Lis = [-21.98, 32.65, -39.29, -46.15 , -39.97] # List Declaration

print('Python TRUNC() Function on Positive Decimal = %d' %math.trunc(21.98763))
print('Python TRUNC() Function on Negative Decimal = %d' %math.trunc(-15.48476))

print('Python TRUNC() Function on Tuple Item = %d' %math.trunc(Tup[2]))
print('Python TRUNC() Function on Tuple Item = %d' %math.trunc(Tup[4]))
print('Python TRUNC() Function on List Item = %d' %math.trunc(Lis[2]))
print('Python TRUNC() Function on List Item = %d' %math.trunc(Lis[4]))

print('Python TRUNC() Function on Multiple Number = %d' %math.trunc(21 + 19 - 40.6578))

print('Python TRUNC() Function on String Value = ', math.trunc('2.95'))

Output


Python TRUNC() Function on Positive Decimal = 21
Python TRUNC() Function on Negative Decimal = -15
Python TRUNC() Function on Tuple Item = 11
Python TRUNC() Function on Tuple Item = 50
Python TRUNC() Function on List Item = -39
Python TRUNC() Function on List Item = -39
Python TRUNC() Function on Multiple Number = 0
Traceback (most recent call last):
  File "app.py", line 16, in <module>
    print('Python TRUNC() Function on String Value = ', math.trunc('2.95'))
TypeError: type str doesn't define __trunc__ method


Thanks for reading !

#python #programming

Introduction Python trunc() Method with Examples
1 Likes11.10 GEEK