Truncating Floats in Python: A Guide to 2 Decimal Precision
Rounding numbers is a common task in programming, particularly when dealing with floatingpoint numbers. In Python, you might encounter situations where you need to truncate a float to a specific number of decimal places, for instance, displaying currency values or formatting data for presentation. This article will guide you through the process of truncating floats to 2 decimal places in Python.
Let's imagine you have a Python program calculating the price of a product after applying a discount. You want to display the final price with only two decimal places. The code snippet below demonstrates this situation:
original_price = 12.99
discount_percentage = 10
discount = original_price * (discount_percentage / 100)
final_price = original_price  discount
print(f"The final price is: {final_price}")
This code calculates the discount and final price, but the output will display the final price with many decimal places. You can use the round()
function to truncate the float to two decimal places:
original_price = 12.99
discount_percentage = 10
discount = original_price * (discount_percentage / 100)
final_price = original_price  discount
final_price_truncated = round(final_price, 2)
print(f"The final price is: {final_price_truncated}")
The round()
function takes two arguments: the number to be rounded and the number of decimal places to keep. In this case, round(final_price, 2)
rounds the final_price
to two decimal places.
Understanding Rounding and Truncation
It's important to understand the difference between rounding and truncation:
 Rounding: Rounds a number to the nearest value based on the specified decimal places.
 Truncation: Simply removes the digits after the specified decimal place without considering the next digit.
For example, rounding 1.2345 to two decimal places would result in 1.23, while truncating it would result in 1.23.
Alternative Methods for Truncation
While round()
is the most common method for achieving twodecimal precision, other options exist, including:

String formatting:
final_price_truncated = "{:.2f}".format(final_price) print(f"The final price is: {final_price_truncated}")

fstring formatting:
final_price_truncated = f"{final_price:.2f}" print(f"The final price is: {final_price_truncated}")
Both methods format the float as a string with two decimal places. However, it's crucial to remember that these methods return a string, not a float. If you need to perform calculations with the truncated number, you'll have to convert it back to a float using the float()
function.
Choosing the Right Method
The best method for truncating floats to two decimal places depends on your specific needs and preferences.
round()
is a versatile function that provides both rounding and truncation functionalities. String formatting is a convenient way to format numbers for display, but it returns a string.
fstring
formatting is a more concise and modern alternative to string formatting.
Ultimately, choose the method that best suits your coding style and project requirements.
Conclusion
This article explored various methods for truncating floats to two decimal places in Python. Understanding these methods and their nuances will empower you to effectively manipulate floatingpoint numbers and format your results accurately. Remember, choosing the right method for your specific scenario is key to achieving the desired output and maintaining code clarity.