Python

# Python Math Trunc

When truncating a number, we discard the fractional part to get an integer value of that number. The numbers may contain a fractional part that we couldn’t need in some programs. These are raw numbers that may overload the output or have not yet been processed.

There are two ways to eliminate the decimal digits from a given value in Python. The math.trunc() method truncates the number of its arguments. The int() method changes a string or given number to an integer. In this process, Python discards the fractional part of that given value.

If we truncate a given value, the number before the decimal point does not change. Truncation may give the same outcome as rounding a value. But these two are different. When rounded, 7.89 becomes 8, but when truncated, it becomes 7. Let’s have a look at how we truncate the given value in Python.

## Truncate the value:

The math.trunc() method returns an integer trimmed from the passed numeric argument. This truncates some of the values towards zero. The fractional element of the passed value is removed.

We employ version 5 of the ‘spyder’ tool for the implementation. Let’s get started coding now. In this case, we import the math module.

We take the number 15.4689. We apply the function math.trunc(). This value is given to the function as a parameter. This program must now be executed. From spyder’s menu bar, we pick the ‘run’ option.

The print statement prints the truncated value of 15.4689, which is 15.

## Truncate numbers by the use of math.trunc():

In this code, we see how we utilize math.trunc() in this program. Here we truncate four floating-point numbers. We introduce the math module. This is how math.trunc() becomes accessible. Next, we create four variables from number1 to number4. Set this to different floating-point numbers.

Then we use math.trunc() method to truncate these four numbers. The resultant number is what we entered in four new variables. In this code, the last part prints the result. We use the print() function to print the original and truncated values.

The values cannot be rounded up. Otherwise, 467.9457402 would be 468. Also, we don’t round down as it may have changed from -23.8740858 to -24. This demonstrates that truncation is disparate by rounding up and down the numbers.

## Truncate values by the use of int () function:

Another possibility for truncation is the use of the int() method. This method changes the passed argument to an integer. In this procedure, some of the values ​​will be discarded. The function int() truncates the given number towards zero for floating-point values. To utilize the int() function, we pass one argument.

We take an example in which we truncate different floating-point numbers to an integer by using the int() function.

First, we take five different floating-point numbers. Then we create five variables for these numbers. Now we want to truncate these values. We apply the int() function to every variable to do this. We pass the floating-point numbers as an argument to the int() function. Next, we store the resulting integer in five new variables.

The last part of the code uses a print() statement to print the real and truncated values.

## Truncate a list in Python:

In the examples as mentioned above, only one value is truncated at a time. However, we can also truncate a list or array in Python.

Here we import the module. We generate a floating-point values list. We take seven numbers in this list. The variable in which this list is stored is ‘numbers.’ Now, we want to truncate each value in the list. We call it the math.tunc() function for each value in the list. We use the “for” loop. These values ​​are generated individually. The output of this code is stored in the new variable ‘truncnumbers.’

We have two lists after running the code: the original list and the truncated list. In the output, we see that the digits present after the decimal point have been removed from the entered values. If we do not want the values of the original list in output, we can overwrite them with the truncated values.

## Conclusion:

If we want to truncate a value, delete the decimal value. 34.5 becomes 34, and 6.94 becomes 6. The number preceding the decimal point does not change compared to rounding. We looked at many approaches for truncating numbers in Python in this article. There are two techniques in Python to truncate numbers. The first approach is to use math.trunc() method. This method takes a single argument: the number to be trimmed. This value is returned as a number. Another possibility is the use of the int () method. If we specify an argument to this function, that parameter is truncated to an integer. If we take a floating-point number represented as a string, the int() methods first change the given number and then eliminate the decimal value. We have utilized list comprehension for truncating an array or list in Python.