5 Best Ways to Perform Truncation in Python

πŸ’‘ Problem Formulation: When working with numerical data in Python, a common requirement is to truncate numbers to remove the fractional parts without rounding. This article explores various methods to perform truncation in Python. For instance, converting the input 123.456 to 123 is a typical truncation task.

Method 1: Using the math.trunc() Function

This method involves the math.trunc() function from Python’s math module, which explicitly truncates the decimal part of a number and returns the integer value. It is a straightforward and mathematically clear approach to truncation.

Here’s an example:

import math

number = 123.456
truncated_number = math.trunc(number)
print(truncated_number)

Output: 123

This code snippet imports the math module, then uses the math.trunc() function to truncate the number 123.456 and prints the result, which is the integer 123.

Method 2: Truncation Using Integer Typecasting

Another common way to truncate a floating-point number in Python is to typecast it to an integer using the built-in int() function. This is a simple and quick method for removing the decimal part.

Here’s an example:

number = 123.456
truncated_number = int(number)
print(truncated_number)

Output: 123

In this code, int() is used to cast the float 123.456 to an integer, which naturally truncates the decimal part, resulting in 123 being printed.

Method 3: Truncation with String Manipulation

This unique method involves converting the number to a string and splitting it at the decimal point to truncate it. Then, the integer part before the decimal is converted back to an integer. This method is a bit more roundabout but can be useful in specific contexts.

Here’s an example:

number = 123.456
truncated_number = int(str(number).split('.')[0])
print(truncated_number)

Output: 123

The code converts the number to a string, splits it into a list at the point where the decimal occurs, takes the first element of that list (the part before the decimal point), and converts it back to an integer.

Method 4: Using the floor() Function for Non-Negative Numbers

For non-negative numbers, you can use the floor() function from the math module to truncate a float. The floor() function takes a float and returns the largest integer less than or equal to the float.

Here’s an example:

import math

number = 123.456
truncated_number = math.floor(number)
print(truncated_number)

Output: 123

This snippet uses math.floor() to get the nearest integer less than or equal to 123.456. It’s important to note that floor() behaves like truncation for non-negative numbers but not for negative numbers.

Bonus One-Liner Method 5: Truncate with Lambda Function

For a more flexible and inline approach, a lambda function can be defined to truncate a number using the previously mentioned integer typecast. This single-line method can be especially convenient when you need a small, anonymous function for truncating on-the-go.

Here’s an example:

truncate = lambda x: int(x)
print(truncate(123.456))

Output: 123

This one-liner creates a lambda function that takes a number x and casts it to an integer, thereby truncating it. The function is then immediately used to truncate 123.456.

Summary/Discussion

  • Method 1: math.trunc(). Direct and mathematically explicit. Not the most concise.
  • Method 2: Integer Typecasting. Extremely simple and concise. Implicit rather than explicit truncation.
  • Method 3: String Manipulation. Versatile and hackable. Indirect and less performant.
  • Method 4: math.floor(). Accurate for non-negative numbers. Misleading when used with negatives.
  • Bonus Method 5: Lambda Function. Quick and inline. Can be less readable to those unfamiliar with lambda functions.