← Python lessons

Python round(): Round to 2 Decimal Places (and Why 0.1 + 0.2 ≠ 0.3)

Part of a free Python course — sign up to save your progress and get AI hints.Sign up free

Python's round() rounds a number to a whole number, or to a set number of decimal places.

Read Data Types and Operations first.

Round to decimal places

round(value, n) rounds to n decimal places. Leave out n to get a whole number.

average = 37 / 3
print(average)
print(round(average, 2))
print(round(average))

This prints 12.333333333333334, then 12.33, then 12. With no n, the result is an integer.

Rounding ties go to the even number

When a number is exactly halfway, round() picks the even neighbour. This is called banker's rounding.

print(round(2.5))
print(round(3.5))
print(round(4.5))

This prints 2, 4 and 4, not 3, 4 and 5. It stops halfway values always pushing totals up.

Why 0.1 + 0.2 is not 0.3

Floats are stored in binary, so most decimals are tiny approximations. Small errors can show up.

total = 0.1 + 0.2
print(total)
print(total == 0.3)
print(round(total, 2) == 0.3)

This prints 0.30000000000000004, False, then True. In this example, rounding to two decimal places gives the same float as 0.3. Rounding is not a general fix for comparing floats: two very close values can round to different results if they fall on opposite sides of a rounding boundary.

The same error explains a surprise: round(2.675, 2) gives 2.67. The stored value is just under 2.675.

Rounding versus formatting

round() changes the value. It does not add trailing zeroes. To always show two places, format the number instead.

price = 2.0
print(round(price, 2))
print(f"{price:.2f}")

This prints 2.0, then 2.00. Use round() for maths, and :.2f for display. The formatted version is a string.

Note: For exact money sums, real programs often use the decimal module or count whole cents as integers. This lesson leaves those out.

See also: String formatting.

Your turn

Print total rounded to 2 decimal places. Then print total / 3 as text with exactly 2 decimal places.

total = 10.0 + 12.555 + 15.0
# add two prints: total rounded to 2 places, then total / 3 formatted with :.2f

Expected output

37.55
12.52