How to Round in Python

What you’ll build or solve

You’ll round numbers in Python using round(), format rounding for display, and control how many decimal places you keep.

When this approach works best

This approach works best when you:

  • Show cleaner numbers in a UI, report, or chart, like rounding 3.14159 to 3.14.
  • Prepare data for summaries, like rounding averages or percentages.
  • Normalize values before comparisons, like treating 9.999 as 10.0 at a chosen precision.

Avoid this approach when:

  • You need exact decimal arithmetic for currency or billing rules. Use decimal.Decimal and an explicit rounding mode.

Prerequisites

  • Python installed
  • You know what a number and a function are

Step-by-step instructions

1) Round to the nearest whole number with round()

round(x) returns a number rounded to the nearest integer.

print(round(3.2))
print(round(3.8))

What to look for: The result is an int when you call round() with one argument.


2) Round to a fixed number of decimal places

Pass the number of decimal places as the second argument.

pi=3.14159
print(round(pi,2))
print(round(pi,4))

This is common for percentages and averages:

rate=0.123456
print(round(rate*100,1))

3) Understand how .5 rounds in Python

Python uses “bankers rounding,” also called “round half to even.” Ties round to the nearest even result.

print(round(2.5))# 2
print(round(3.5))# 4

This can surprise people who expect .5 to always round up.

What to look for: This tie behavior can also appear when rounding to decimals, depending on how a float is stored internally.


4) Round up or down with math.ceil() and math.floor()

Use ceil() to always round up and floor() to always round down.

importmath

print(math.ceil(3.1))
print(math.floor(3.9))

This is useful for counts, pages, and quotas:

importmath

items=53
page_size=10
pages=math.ceil(items/page_size)
print(pages)

5) Round for display with formatting

If you only want to control how a number looks, format it instead of changing the underlying value.

value=3.14159
text=f"{value:.2f}"
print(text)

You can also use format():

value=3.14159
print(format(value,".2f"))

What to look for: Formatted output is a string, not a number.


Examples you can copy

Example 1: Round a user score to one decimal

score=9.876
print(round(score,1))

Example 2: Always round up to the next whole item

importmath

hours=2.1
billed_hours=math.ceil(hours)
print(billed_hours)

Example 3: Format a percentage for display

rate=0.07654
print(f"{rate*100:.1f}%")

Example 4: Use Decimal for predictable money rounding

Use this when exact decimal arithmetic matters, like for billing.

fromdecimalimportDecimal,ROUND_HALF_UP

price=Decimal("2.675")
rounded=price.quantize(Decimal("0.01"),rounding=ROUND_HALF_UP)
print(rounded)

Common mistakes and how to fix them

Mistake 1: Expecting round(2.5) to return 3

What you might do:

print(round(2.5))

Why it breaks: Python rounds ties to the nearest even number.

Correct approach: Pick a rule, or use Decimal for fixed rules.

fromdecimalimportDecimal,ROUND_HALF_UP

value=Decimal("2.5")
print(value.quantize(Decimal("1"),rounding=ROUND_HALF_UP))

Mistake 2: Rounding for display but accidentally changing the value type

What you might do:

value=3.14159
value=f"{value:.2f}"
print(value+1)

Why it breaks: Formatting produces a string, so math operations fail.

Correct approach: Keep numbers as numbers, format only at the edge.

value=3.14159
rounded_value=round(value,2)
print(rounded_value+1)

Or format only when printing:

value=3.14159
print(f"{value:.2f}")

Troubleshooting

If round() gives results like 2.675 -> 2.67, do this: treat it as a float representation issue, and use Decimal for money-like rules.

If .5 rounds “down” sometimes, do this: remember ties round to even, or choose a Decimal rounding mode.

If you need always-up rounding, do this: use math.ceil().

If you need always-down rounding, do this: use math.floor().

If you want pretty output but still need the real value, do this: keep the number, format only when printing.


Quick recap

  • Use round(x) for whole-number rounding.
  • Use round(x, n) for n decimal places.
  • Expect ties to round to even with round().
  • Use math.ceil() and math.floor() for always-up or always-down rounding.
  • Format with :.2f for display without changing the value.
  • Use Decimal when you need strict decimal rounding rules.