How to Round Numbers in Python (Up, Down, Nearest)

By default, Python’s round() goes to the nearest whole number and sends ties to the even one, so round(2.5) is 2. To round numbers up, use math.ceil(); to round down, math.floor().

If you still round with the old int(n + 0.5) trick, stop: it gives the wrong answer for -12.7. Python’s decimal module does half up properly.

In this tutorial, I’ll show you how to round numbers in Python five ways: nearest, up, down, to a step such as 5 or 0.25, and half up. I ran all of it on Python 3.12.5.

The Road-Trip Numbers We’ll Round

All the examples share one small file. Here’s how to set it up:

  1. In VS Code, open an empty folder and create trip.py.
  2. Paste the code below and save it.
  3. Save each example in the same folder and run it in the VS Code terminal, for example python round_nearest.py.
# Numbers from an Austin to Denver road trip
TRIP = {
    "miles": 934.5,
    "hours": 13.2,
    "hotel": 412.37,
    "tolls": 7.85,
    "temp change": -12.7,
}

Three values are there to cause trouble: 934.5 is an exact tie, 7.85 can’t be stored exactly as a float, and -12.7 is negative.

Method 1: Round to the Nearest Whole Number with round()

Start here when you just want the closest whole number. It’s built in, so there’s nothing to import. Save this as round_nearest.py and run it in the VS Code terminal:

from trip import TRIP

for name, n in TRIP.items():
    print(f"{name:<12}{n:>8} -> {round(n)}")

print("type:", type(round(13.2)).__name__)
print("ties:", [round(x) for x in (0.5, 1.5, 2.5, 3.5)])

Output:

miles          934.5 -> 934
hours           13.2 -> 13
hotel         412.37 -> 412
tolls           7.85 -> 8
temp change    -12.7 -> -13
type: int
ties: [0, 2, 2, 4]
Python round number example: VS Code output of round() on five road-trip numbers and four ties
934.5 becomes 934, and the ties 0.5, 1.5, 2.5 and 3.5 become 0, 2, 2 and 4.

How does this code work?

With one argument, round() returns an int. 13.2 goes to 13, 7.85 goes to 8, and -12.7 goes to -13 because that’s the closer whole number.

934.5 sits exactly between 934 and 935, so Python picks the even one. This is called banker’s rounding, and it stops long lists of ties from always drifting upward.

Add a second argument for decimal places, such as round(412.37, 1). My Python round() function tutorial covers the full syntax.

Method 2: How to Round Up in Python with math.ceil()

Use this when a part counts as a whole: tanks of gas, boxes, pages, billable hours. Same road-trip numbers, but now everything moves up. Save it as round_up.py and run it in the VS Code terminal:

import math
from trip import TRIP

for name, n in TRIP.items():
    print(f"{name:<12}{n:>8} -> {math.ceil(n)}")

# Round up to the next 10
print("hotel, next 10:", math.ceil(TRIP["hotel"] / 10) * 10)

# Whole tanks of gas at 350 miles per tank
print("tanks:", math.ceil(TRIP["miles"] / 350))

Output:

miles          934.5 -> 935
hours           13.2 -> 14
hotel         412.37 -> 413
tolls           7.85 -> 8
temp change    -12.7 -> -12
hotel, next 10: 420
tanks: 3
Python round up example: VS Code output of math.ceil() on road-trip numbers, the next 10 and tanks of gas
math.ceil() turns 13.2 hours into 14, and the 934.5-mile trip needs 3 tanks.

How does this code work?

math.ceil() returns the smallest whole number that isn’t below yours. So 412.37 becomes 413, and -12.7 becomes -12, because -12 is the higher number. More cases are in the ceil() function in Python.

To round up to the next 10, divide by 10, take the ceiling, then multiply back: 41.237 becomes 42, which gives 420.

Important: float noise can push you one too high. 1.1 * 100 is 110.00000000000001 in Python, so math.ceil(1.1 * 100) returns 111. Clean it first: math.ceil(round(1.1 * 100, 6)).

Method 3: Round Down with math.floor() or int()

Rounding down is for what you’ve fully completed, like whole hours driven. Python has three tools for it, and they only disagree on negatives. Same trip data; save it as round_down.py and run it in the VS Code terminal:

import math
from trip import TRIP

print(f"{'':<12}{'number':>8}{'floor':>7}{'trunc':>7}{'int':>6}")
for name, n in TRIP.items():
    low, cut = math.floor(n), math.trunc(n)
    print(f"{name:<12}{n:>8}{low:>7}{cut:>7}{int(n):>6}")

Output:

              number  floor  trunc   int
miles          934.5    934    934   934
hours           13.2     13     13    13
hotel         412.37    412    412   412
tolls           7.85      7      7     7
temp change    -12.7    -13    -12   -12
Python round down comparison: VS Code table of math.floor(), math.trunc() and int() on five numbers
The positive rows match. For -12.7, floor gives -13 while trunc and int give -12.

How does this code work?

math.floor() always goes lower, so -12.7 drops to -13. math.trunc() and int() just cut off the decimals, which moves a negative number toward zero.

Dividing first? 934.5 // 350 already floors the result and returns 2.0. See normal division vs floor division in Python.

Method 4: Round to the Nearest 5, 10, 100 or Quarter in Python

Estimates and price tags often need a step, not a decimal place. For tens and hundreds, give round() a negative second argument. For anything else, a tiny helper does it.

Here it is on the same $412.37 hotel bill. Save it as round_step.py and run it in the VS Code terminal:

import math
from trip import TRIP

def round_to(n, step, how=round):
    return round(how(n / step) * step, 2)

hotel = TRIP["hotel"]
print("nearest 10: ", round(hotel, -1))
print("nearest 100:", round(hotel, -2))
print("nearest 5:  ", round_to(hotel, 5))
print("quarter:    ", round_to(hotel, 0.25))
print("nickel:     ", round_to(hotel, 0.05))
print("up to 5:    ", round_to(hotel, 5, math.ceil))
print("down to 5:  ", round_to(hotel, 5, math.floor))

Output:

nearest 10:  410.0
nearest 100: 400.0
nearest 5:   410
quarter:     412.25
nickel:      412.35
up to 5:     415
down to 5:   410
Python round to nearest 5, 10, 100, quarter and nickel: VS Code output for the number 412.37
412.37 becomes 410 at the nearest 5 but 415 when you pass math.ceil.

How does this code work?

The helper counts steps: 412.37 / 5 is 82.474, which rounds to 82 steps, and 82 * 5 is 410. With a 0.25 step, 412.25 wins over 412.50 because it’s one cent closer.

how is a function you pass in, so math.ceil and math.floor reuse the same code. That’s the idea behind higher-order functions in Python.

The outer round(..., 2) removes float noise. Without it, rounding 7.12 to the nearest 0.05 gives 7.1000000000000005.

Method 5: Round Half Up with Python’s decimal Module

This is the rounding you learned in school: a 5 always goes up. I’d pick it for invoices and anything a customer checks by hand. Same sample, only the tricky values this time. Save it as round_half_up.py and run it in the VS Code terminal:

from decimal import Decimal, ROUND_HALF_UP
from trip import TRIP

def half_up(n, places="1"):
    exact = Decimal(str(n))
    return exact.quantize(Decimal(places), rounding=ROUND_HALF_UP)

print(f"{'number':>7}{'round':>7}{'int+0.5':>9}{'half_up':>9}")
for n in (TRIP["miles"], TRIP["temp change"]):
    print(f"{n:>7}{round(n):>7}{int(n + 0.5):>9}{half_up(n):>9}")

tolls = TRIP["tolls"]
print("tolls, 1 decimal:", round(tolls, 1), "vs", half_up(tolls, "0.1"))

Output:

 number  round  int+0.5  half_up
  934.5    934      935      935
  -12.7    -13      -12      -13
tolls, 1 decimal: 7.8 vs 7.9
Python round half up: VS Code output comparing round(), int(n + 0.5) and Decimal ROUND_HALF_UP
half_up() returns 935 for the tie and -13 for -12.7; int(n + 0.5) gets -12.7 wrong.

How does this code work?

quantize(Decimal("1")) means “keep no decimals”, and Decimal("0.1") means “keep one”. ROUND_HALF_UP then sends 934.5 to 935.

str(n) matters. The float 7.85 is really stored a hair below 7.85, so round(7.85, 1) gives 7.8. Going through text keeps the exact 7.85, and you get 7.9.

For negative ties, ROUND_HALF_UP moves away from zero, so -2.5 becomes -3. For cents, see rounding numbers to 2 decimal places in Python.

Is There a math.round() Function in Python?

No. JavaScript has Math.round(), but in Python round() is a built-in, and the math module only has ceil(), floor() and trunc(). You can check it: save this as math_round.py and run it in the VS Code terminal:

import math

print([name for name in ("round", "ceil", "floor", "trunc")
       if hasattr(math, name)])
try:
    math.round(13.2)
except AttributeError as err:
    print("AttributeError:", err)
print(round(13.2))

Output:

['ceil', 'floor', 'trunc']
AttributeError: module 'math' has no attribute 'round'
13