Python Program for trunc() Function

In the previous article, we have discussed Python Program for ldexp() Function
trunc() Function in Python:

The math.trunc() method returns the integer part of a number that has been truncated.

Note: Please keep in mind that this method will NOT round the number up or down to the nearest integer, but will simply remove the decimals.

Syntax:

math.trunc(x)

Parameters:

x: This is required. It is a number. The number from which you want to remove the decimal point(truncate). If the value is not a number, a TypeError is returned.

Return Value: Returns an int value that represents a number’s truncated integer parts.

Examples:

Example1:

Input:

Given first number = 870.26
Given second number = -11.56

Output:

The truncated value of the given first number  870.26  =  870
The truncated value of the given second number  -11.56  =  -11

Example2:

Input:

Given first number = 80.432
Given second number = 22.4

Output:

The truncated value of the given first number  80.432  =  80
The truncated value of the given second number  22.4  =  22

Program for trunc() Function in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import math module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Give the first number as static input and store it in another variable.
  • Apply math.trunc() function to the given first number to remove the decimal part and give only the integer part of the given number. (truncate)
  • Store it in another variable.
  • Print the truncated value of the given first number.
  • Similarly, do the same for the other number.
  • Apply math.trunc() function to the given list element and print it.
  • The Exit of Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the list as static input and store it in a variable.
gvn_lst = [12.005, 11.89, 30.224]
# Give the first number as static input and store it in another variable.
gvn_numb1 = 870.26
# Apply math.trunc() function to the given first number to remove the decimal
# part and give only the integer part of the given number. (truncate) 
# Store it in another variable.
trnc_numb = math.trunc(gvn_numb1)
# Print the truncated value of the given first number.
print("The truncated value of the given first number ",
      gvn_numb1, " = ", trnc_numb)
# similarly do the same for the other number.
gvn_numb2 = -11.56
trnc_numb2 = math.trunc(gvn_numb2)
print("The truncated value of the given second number ",
      gvn_numb2, " = ", trnc_numb2)
# Apply math.trunc() function to the given list element and print it.
print(
    "The truncated value of the given list element ", gvn_lst[2], " = ", math.trunc(gvn_lst[2]))

Output:

The truncated value of the given first number  870.26  =  870
The truncated value of the given second number  -11.56  =  -11
The truncated value of the given list element  30.224  =  30

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import math module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the first number as user input using the float(input()) function and store it in a variable.
  • Apply math.trunc() function to the given first number to remove the decimal part and give only the integer part of the given number. (truncate)
  • Store it in another variable.
  • Print the truncated value of the given first number.
  • Similarly, do the same for the other number.
  • Apply math.trunc() function to the given list element and print it.
  • The Exit of Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(float, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Give the first number as user input using the float(input()) function
# and store it in a variable.
gvn_numb1 = float(input("Enter some random number = "))
# Apply math.trunc() function to the given first number to remove the decimal
# part and give only the integer part of the given number. (truncate) 
# Store it in another variable.
trnc_numb = math.trunc(gvn_numb1)
# Print the truncated value of the given first number.
print("The truncated value of the given first number ",
      gvn_numb1, " = ", trnc_numb)
# similarly do the same for the other number.
gvn_numb2 = float(input("Enter some random number = "))
trnc_numb2 = math.trunc(gvn_numb2)
print("The truncated value of the given second number ",
      gvn_numb2, " = ", trnc_numb2)
# Apply math.trunc() function to the given list element and print it.
print(
    "The truncated value of the given list element ", gvn_lst[1], " = ", math.trunc(gvn_lst[1]))

Output:

Enter some random List Elements separated by spaces = 3 45.66 20.01 54
Enter some random number = 80.432
The truncated value of the given first number 80.432 = 80
Enter some random number = 22.4
The truncated value of the given second number 22.4 = 22
The truncated value of the given list element 45.66 = 45

Read all the mathematical functions available in Python and understand how to implement them in your program by using the tutorial of Python Mathematical Methods Examples.