[Python] 3 Ways to Limit Decimal Places in Python (With Examples)
Hello, this is BlockDMask.
In this post, we'll explore how to control the number of decimal places in Python.
Whether you're formatting prices, displaying percentages, or rounding calculations — precision matters.
<Table of Contents>
- Using the round() function
- Using the format() function
- Using f-strings
- 💡Bonus Tip: Using decimal for accurate rounding
1. Using the round() Function
The round()
function is the most basic way to round a number to a certain number of decimal places.
It takes two arguments: the number to round, and the number of decimal places.
price1 = round(19.8765) # 20
price2 = round(19.8765, 0) # 20.0
price3 = round(19.8765, 1) # 19.9
price4 = round(19.8765, 2) # 19.88
price5 = round(19.8765, 3) # 19.877
print(f"Rounded (no decimal): {price1}")
print(f"Rounded (0 decimals): {price2}")
print(f"Rounded (1 decimal): {price3}")
print(f"Rounded (2 decimals): {price4}")
print(f"Rounded (3 decimals): {price5}")
Rounded (no decimal): 20
Rounded (0 decimals): 20.0
Rounded (1 decimal): 19.9
Rounded (2 decimals): 19.88
Rounded (3 decimals): 19.877
Explanation:
round(number, digits)
rounds a float to the specified number of digits after the decimal point.
If no digits are provided, it rounds to the nearest integer.
2. Using the format() Function
You can also control decimal precision using the format()
method on strings.
This is useful when printing values with specific formatting like currency, ratings, or percentages.
temp = "Temperature: {:.1f}°C".format(23.6789)
rating = "Rating: {:.2f}/5".format(4.456)
block_price = "BlockDMask Price: ${:.3f}".format(149.98765)
print(temp)
print(rating)
print(block_price)
Temperature: 23.7°C
Rating: 4.46/5
BlockDMask Price: $149.988
Explanation:
The {:.2f}
syntax means: show 2 digits after the decimal point, and round accordingly.
You can also use index-based formatting:
a = "First: {0:.1f}, Second: {1:.2f}".format(3.145, 7.891)
print(a) # First: 3.1, Second: 7.89
3. Using f-strings (Python 3.6+)
f-strings are a cleaner and faster way to format strings, introduced in Python 3.6.
You can apply the same decimal control syntax directly within curly braces.
discount = 0.15678
tax_rate = 7.12543
print(f"BlockDMask Discount: {discount:.2f}") # 0.16
print(f"Tax Rate: {tax_rate:.1f}%") # 7.1%
print(f"Tax Rate (3 decimals): {tax_rate:.3f}") # 7.125
BlockDMask Discount: 0.16
Tax Rate: 7.1%
Tax Rate (3 decimals): 7.125
Explanation:
With f-strings, just use {value:.Nf}
to limit to N decimal places. It’s fast, clean, and readable.
💡 Bonus Tip: Use decimal for precise financial rounding
If you’re working with money, Python’s built-in float can introduce small inaccuracies.
Use the decimal
module for exact decimal representation and rounding.
from decimal import Decimal, ROUND_HALF_UP
price = Decimal("2.675")
rounded_price = price.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
print(f"Accurate financial rounding: {rounded_price}")
Accurate financial rounding: 2.68
Explanation:
Standard floats may give unexpected results like 2.675 → 2.67.
The decimal
module avoids this by using base-10 math and strict rounding rules.
That’s it!
We’ve covered 3 different ways to round or limit decimal places in Python — using round()
, format()
, and f-strings
, plus a bonus tip with the decimal
module.
Use the method that best fits your case — whether it's displaying prices, scores, or calculations.
Thanks for reading! – BlockDMask
Comments
Post a Comment