Python
Rounding
Programming
Code Examples
Software Development

Round to 5 or other number in Python

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Rounding numbers is one of the most frequent operations in everyday programming, whether you are formatting prices, bucketing data for histograms, or snapping values to a grid. Python's built-in round() function handles simple decimal rounding, but rounding to the nearest 5, 10, or any arbitrary multiple requires a small extra step. This article walks through both standard rounding and custom-multiple rounding with clear examples you can use immediately.

Python's Built-In round() Function

The round() function takes a number and an optional precision argument that specifies how many decimal places to keep:

python
1# Round to the nearest integer
2print(round(3.7))       # 4
3print(round(3.2))       # 3
4
5# Round to 2 decimal places
6print(round(3.14159, 2))  # 3.14
7
8# Round to 1 decimal place
9print(round(2.75, 1))     # 2.8

One important behavior to know: Python uses banker's rounding (round half to even) for values that fall exactly on the midpoint. This means round(2.5) returns 2, not 3, because the rule rounds to the nearest even number to reduce cumulative bias in large datasets:

python
1print(round(0.5))   # 0
2print(round(1.5))   # 2
3print(round(2.5))   # 2
4print(round(3.5))   # 4

Rounding to the Nearest Multiple of Any Number

To round to the nearest 5, 10, 25, or any other integer multiple, use a simple formula: divide by the multiple, round, then multiply back:

python
1def round_to_multiple(value, multiple):
2    """Round value to the nearest multiple."""
3    return multiple * round(value / multiple)
4
5print(round_to_multiple(7, 5))     # 5
6print(round_to_multiple(8, 5))     # 10
7print(round_to_multiple(23, 10))   # 20
8print(round_to_multiple(27, 10))   # 30
9print(round_to_multiple(13, 25))   # 25

This works because dividing by the multiple normalizes the value so that round() can do its job, and multiplying back restores the original scale.

Rounding Up or Down to a Multiple

Sometimes you need to always round up (ceiling) or always round down (floor) rather than rounding to the nearest value. The math module provides the tools for this:

python
1import math
2
3def round_up_to_multiple(value, multiple):
4    """Always round up to the next multiple."""
5    return multiple * math.ceil(value / multiple)
6
7def round_down_to_multiple(value, multiple):
8    """Always round down to the previous multiple."""
9    return multiple * math.floor(value / multiple)
10
11print(round_up_to_multiple(7, 5))     # 10
12print(round_down_to_multiple(7, 5))   # 5
13print(round_up_to_multiple(21, 10))   # 30
14print(round_down_to_multiple(21, 10)) # 20

The ceiling approach is useful in scenarios like calculating the number of pages needed to display items (you always want to round up so no items are left out) or pricing tiers that snap to the next increment.

Rounding Negative Numbers

Rounding negative numbers introduces a subtlety. math.floor always rounds toward negative infinity, while math.ceil always rounds toward positive infinity:

python
1import math
2
3print(math.floor(-7.3))   # -8  (toward negative infinity)
4print(math.ceil(-7.3))    # -7  (toward positive infinity)
5print(round(-7.5))        # -8  (banker's rounding: nearest even)

If you need to round away from zero (the "schoolbook" rounding rule), use the Decimal module:

python
1from decimal import Decimal, ROUND_HALF_UP
2
3result = Decimal('-2.5').quantize(Decimal('1'), rounding=ROUND_HALF_UP)
4print(result)  # -3

Practical Example: Snapping Prices

A common real-world task is snapping prices to "friendly" values, like rounding to the nearest $0.25:

python
1def snap_price(price, increment=0.25):
2    """Snap a price to the nearest increment."""
3    return round(price / increment) * increment
4
5prices = [1.07, 2.33, 4.88, 9.99]
6snapped = [snap_price(p) for p in prices]
7print(snapped)  # [1.0, 2.25, 4.75, 10.0]

Notice that the same round_to_multiple formula works with floating-point multiples as well as integers.

Common Pitfalls

  • Expecting round(2.5) to return 3. Python's banker's rounding returns 2 because it rounds to the nearest even number. If you need "always round up on .5," use Decimal with ROUND_HALF_UP.
  • Using integer division (//) instead of true division (/). When both operands are integers, // truncates the result before the round() call ever sees it, producing incorrect output. Always ensure at least one operand is a float, or use /.
  • Floating-point precision surprises. Expressions like round(2.675, 2) return 2.67 instead of the expected 2.68 because 2.675 cannot be represented exactly in binary floating point. Use Decimal for financial calculations where exact rounding matters.
  • Forgetting to handle zero as the multiple. Passing multiple=0 to the round_to_multiple function causes a ZeroDivisionError. Always validate inputs or set a sensible default.
  • Rounding too early in a chain of calculations. Rounding intermediate values accumulates error across steps. Keep full precision through the calculation and round only the final result.

Summary

  • Python's round() uses banker's rounding by default, which rounds midpoint values to the nearest even number.
  • To round to an arbitrary multiple (5, 10, 25, etc.), use the formula multiple * round(value / multiple).
  • Use math.ceil or math.floor combined with the same formula when you need to always round up or always round down.
  • For financial or precision-critical work, use the Decimal module with explicit rounding modes like ROUND_HALF_UP.
  • Always round at the end of a calculation chain, not at intermediate steps, to minimize accumulated floating-point error.

Course illustration
Course illustration

All Rights Reserved.