Python offers several ways to display a number with two digits after the decimal point. The old-style %.2f operator is one; modern f-strings and format() use the same fixed-point precision idea with clearer variable placement. The important distinction is between formatting a representation and changing the numeric value. A two-decimal display is not, by itself, a currency calculation or a precision guarantee.
Start with an exact, runnable comparison
Run these statements in Python 3. They produce a text representation with exactly two digits after the point, including a trailing zero. They do not mutate rate or convert it to a two-decimal numeric type.
rate = 12.5
print("%.2f" % rate)
print(f"{rate:.2f}")
print(format(rate, ".2f"))
print(rate)
# 12.50
# 12.50
# 12.50
# 12.5
The older percent operator interpolates values using a printf-style specifier. The f-string places the format specification after the expression. format() can accept a dynamically selected specification, which is useful in reusable reporting code.
What the two and f actually mean
In .2f, the period introduces precision, 2 requests two digits following the decimal point, and f selects fixed-point notation. The specification is about the resulting string. Width, grouping and alignment can be combined with it when a table needs consistent columns.
values = [4, 25.3, -1.276]
for value in values:
print(f"{value:>9,.2f}")
# 4.00
# 25.30
# -1.28
Display rounding follows the formatter and the represented floating-point value. Do not assume that decimal halves are stored exactly in binary, or that the same printed result proves equal underlying values.
Why round(value, 2) is not the same task
round(value, 2) returns a numeric result. A numeric value does not store a display requirement for trailing zeroes: converting round(12.5, 2) to text still shows 12.5. By contrast f"{12.5:.2f}" returns the string 12.50. Use numeric rounding when an algorithm needs a rounded numeric intermediate, not as a substitute for a presentation format.
x = 12.5
print(round(x, 2)) # 12.5
print(f"{x:.2f}") # 12.50
print(type(f"{x:.2f}")) # <class 'str'>
Use Decimal for specified decimal money rules
Financial calculations often need an explicit decimal representation and a documented tie-breaking rule. Construct Decimal values from strings, perform the arithmetic in decimal, and only then quantize to the required unit. The example below deliberately selects ROUND_HALF_UP; it does not claim this rule is universally appropriate for taxes or accounting.
from decimal import Decimal, ROUND_HALF_UP
amount = Decimal("2.345")
cent = Decimal("0.01")
result = amount.quantize(cent, rounding=ROUND_HALF_UP)
print(result) # 2.35
Different jurisdictions and accounting systems have different rounding rules. Record the rule in your application requirements. Formatting a binary float at the end of a calculation cannot fix earlier measurement or representation errors.
Format negative values, zero and non-finite values intentionally
When producing a report, test negative values, exact zero, values near a rounding boundary, and non-finite data. A formatted negative value close to zero can display as -0.00. If the business specification wants a displayed zero instead, normalize it deliberately rather than silently hiding negative signs in data processing.
for value in (0.0, -0.001, 2.346, float("inf")):
print(f"{value:.2f}")
# 0.00
# -0.00
# 2.35
# inf
Choose a formatting approach by the output
For one-off messages and modern Python code, prefer f-strings. For a reusable rendering function, format(value, specification) is useful when column formats vary. Retain %.2f when maintaining a legacy Python application or explaining printf-style formatting. In CSV output, decide whether the consumer requires a number or a two-decimal text field before encoding it.
Check the official format specification
The Python format specification reference explains the precision, alignment, sign and grouping options and distinguishes fixed-point from significant-digit formatting. Test your chosen rule with actual source data, not only the convenient value 3.14.