{"id":24198,"date":"2021-10-14T09:17:05","date_gmt":"2021-10-14T03:47:05","guid":{"rendered":"https:\/\/python-programs.com\/?p=24198"},"modified":"2021-11-05T20:56:16","modified_gmt":"2021-11-05T15:26:16","slug":"python-program-for-trunc-function","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-trunc-function\/","title":{"rendered":"Python Program for trunc() Function"},"content":{"rendered":"
In the previous article, we have discussed Python Program for ldexp() Function<\/a> The math.trunc() method returns the integer part of a number that has been truncated.<\/p>\n Note:<\/strong> 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.<\/p>\n Syntax:<\/strong><\/p>\n Parameters:<\/strong><\/p>\n x:<\/strong> 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.<\/p>\n Return Value:<\/strong> Returns an int value that represents a number’s truncated integer parts.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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<\/a>.<\/p>\n 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 …<\/p>\n
\ntrunc() Function in Python:<\/strong><\/p>\nmath.trunc(x)<\/pre>\n
Given first number = 870.26\r\nGiven second number = -11.56<\/pre>\n
The truncated value of the given first number 870.26 = 870\r\nThe truncated value of the given second number -11.56 = -11<\/pre>\n
Given first number = 80.432\r\nGiven second number = 22.4<\/pre>\n
The truncated value of the given first number 80.432 = 80\r\nThe truncated value of the given second number 22.4 = 22<\/pre>\n
Program for trunc() Function in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import math module using the import keyword.\r\nimport math\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [12.005, 11.89, 30.224]\r\n# Give the first number as static input and store it in another variable.\r\ngvn_numb1 = 870.26\r\n# Apply math.trunc() function to the given first number to remove the decimal\r\n# part and give only the integer part of the given number. (truncate)\u00a0\r\n# Store it in another variable.\r\ntrnc_numb = math.trunc(gvn_numb1)\r\n# Print the truncated value of the given first number.\r\nprint(\"The truncated value of the given first number \",\r\n gvn_numb1, \" = \", trnc_numb)\r\n# similarly do the same for the other number.\r\ngvn_numb2 = -11.56\r\ntrnc_numb2 = math.trunc(gvn_numb2)\r\nprint(\"The truncated value of the given second number \",\r\n gvn_numb2, \" = \", trnc_numb2)\r\n# Apply math.trunc() function to the given list element and print it.\r\nprint(\r\n \"The truncated value of the given list element \", gvn_lst[2], \" = \", math.trunc(gvn_lst[2]))\r\n<\/pre>\n
The truncated value of the given first number 870.26 = 870\r\nThe truncated value of the given second number -11.56 = -11\r\nThe truncated value of the given list element 30.224 = 30<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import math module using the import keyword.\r\nimport math\r\n# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(float, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Give the first number as user input using the float(input()) function\r\n# and store it in a variable.\r\ngvn_numb1 = float(input(\"Enter some random number = \"))\r\n# Apply math.trunc() function to the given first number to remove the decimal\r\n# part and give only the integer part of the given number. (truncate)\u00a0\r\n# Store it in another variable.\r\ntrnc_numb = math.trunc(gvn_numb1)\r\n# Print the truncated value of the given first number.\r\nprint(\"The truncated value of the given first number \",\r\n gvn_numb1, \" = \", trnc_numb)\r\n# similarly do the same for the other number.\r\ngvn_numb2 = float(input(\"Enter some random number = \"))\r\ntrnc_numb2 = math.trunc(gvn_numb2)\r\nprint(\"The truncated value of the given second number \",\r\n gvn_numb2, \" = \", trnc_numb2)\r\n# Apply math.trunc() function to the given list element and print it.\r\nprint(\r\n \"The truncated value of the given list element \", gvn_lst[1], \" = \", math.trunc(gvn_lst[1]))\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 3 45.66 20.01 54\r\nEnter some random number = 80.432\r\nThe truncated value of the given first number 80.432 = 80\r\nEnter some random number = 22.4\r\nThe truncated value of the given second number 22.4 = 22\r\nThe truncated value of the given list element 45.66 = 45<\/pre>\n
\n