Python round() Doesn't Round Half Up: Why round(2.5) Returns 2 and How to Round Correctly with Decimal
In Python, round(2.5) gives 2 rather than 3, and round(2.675, 2) gives 2.67 rather than 2.68. This article confirms the two causes, round-half-to-even and floating-point error, with actual output, then shows a function that reliably rounds half up with ROUND_HALF_UP from the decimal module, the pitfalls of hand-rolled rounding code, and how JavaScript, Excel and LibreOffice differ.
Japan’s consumption tax, its equivalent of a sales tax or VAT, is 10%, so on an item priced at ¥1,125 before tax it comes to ¥112.5. The yen has no smaller unit in everyday use, so the tax has to be rounded to a whole yen, and rounding half up gives ¥113. A spreadsheet’s ROUND(112.5, 0) also returns 113. Write the same calculation with Python’s round(), though, and you get 112.
>>> round(1125 * 0.1)
112
When someone ports a spreadsheet calculation to a Python script and finds the totals are now off by a few yen, this is usually the reason.
Python’s round() follows a different rule from the half-up rounding most of us learned at school. On top of that, when you ask for a number of decimal places, floating-point error can make a number ending in 5 round in an unexpected direction. This article confirms both causes with actual output, then walks through rounding half up reliably with the decimal module, the pitfalls of the hand-rolled rounding code that circulates online, and how JavaScript and spreadsheets differ.
Unless otherwise noted, the output shown was produced with Python 3.12.3.
1. round() rounds half to even
Pass values that sit exactly halfway between two integers to round() and you get this:
>>> [round(x) for x in (0.5, 1.5, 2.5, 3.5, -0.5, -1.5, -2.5)]
[0, 2, 2, 4, 0, -2, -2]
1.5 becomes 2, 2.5 also becomes 2, and 3.5 becomes 4. A value exactly midway between two integers is rounded to the even one. This is called “round half to even”, or “banker’s rounding”. Values that aren’t at the midpoint, such as 2.6 or 2.4, round to the nearest integer as usual, so the difference only shows up when the last digit is exactly 5.
Python’s official documentation says the same: if two multiples are equally close, rounding is done toward the even choice. Python 2’s round() rounded away from zero, but Python 3.0 switched to this rule (What’s New In Python 3.0). That is why code from older articles can give different results under Python 3.
The point of rounding toward even is to reduce bias when you round many values and add them up. If 0.5 is always rounded up, every midpoint value drifts upward and so does the total. With round half to even, roughly half are rounded up and half down. Taking the opening example and computing the tax on eight items priced from ¥1,125 to ¥1,195 in ¥10 steps makes this easy to see.
| Price before tax | Tax (10%) | round() | Round half up |
|---|---|---|---|
| ¥1,125 | ¥112.5 | ¥112 | ¥113 |
| ¥1,135 | ¥113.5 | ¥114 | ¥114 |
| ¥1,145 | ¥114.5 | ¥114 | ¥115 |
| ¥1,155 | ¥115.5 | ¥116 | ¥116 |
| ¥1,165 | ¥116.5 | ¥116 | ¥117 |
| ¥1,175 | ¥117.5 | ¥118 | ¥118 |
| ¥1,185 | ¥118.5 | ¥118 | ¥119 |
| ¥1,195 | ¥119.5 | ¥120 | ¥120 |
| Total | ¥928 | ¥928 | ¥932 |
Before rounding, the total is ¥928. With round half to even, four values go up and four go down, cancelling out, and the total stays at ¥928. Rounding half up pushes all eight values up by ¥0.5, so the total comes out ¥4 higher. Every value here is exactly at the midpoint, which is an extreme case, but the more midpoint values a dataset contains, the further a half-up total drifts upward. On the other hand, if your business rules say each line on an invoice or report is rounded half up, using round() as is will put half of those lines off by ¥1.
Note also that round() returns an int when you don’t pass a number of digits, while passing 0, as in round(2.5, 0), returns the float 2.0.
2. With a digit count, 2.675 becomes 2.67
If round half to even were the whole story, round(2.675, 2) should return 2.68, since when rounding off the final 5 of 2.675 the even side is 2.68. In practice it returns 2.67.
>>> round(2.675, 2)
2.67
>>> round(1.005, 2)
1.0
This isn’t about the rounding rule but about how the number is represented. Python’s float is a binary floating-point number, and it cannot represent many decimal fractions like 2.675 exactly. When you write 2.675 in source code, what you actually get is the closest binary value. Pass that value to decimal.Decimal and you can see all of its digits.
>>> from decimal import Decimal
>>> Decimal(2.675)
Decimal('2.67499999999999982236431605997495353221893310546875')
>>> Decimal(1.005)
Decimal('1.00499999999999989341858963598497211933135986328125')
What you meant as 2.675 is really 2.67499999…, slightly smaller. It isn’t even at the midpoint, so it rounds to the nearer value, 2.67. The same goes for 1.005. The round() entry in the official documentation also includes a note with this example of round(2.675, 2) giving 2.67 instead of 2.68.
Conversely, for numbers that binary can represent exactly, round half to even shows up as expected. 0.125 is 1/8, so there’s no error.
>>> round(0.125, 2), round(0.375, 2)
(0.12, 0.38)
0.125 went to 0.12 and 0.375 to 0.38, each rounded toward the even side. Even if a value you typed looks like it sits exactly at the midpoint, you can’t tell by looking whether the float is above it, below it or exactly on it.
Formatting to a fixed number of digits with f"{x:.2f}" or format() rounds by the same rule.
>>> f"{2.5:.0f}", f"{3.5:.0f}", f"{2.675:.2f}"
('2', '4', '2.67')
3. Rounding half up with the decimal module
If you want to round half up as a decimal number, use decimal from the standard library. Decimal holds decimal numbers as they are and lets you specify the rounding mode. Rounding half up corresponds to ROUND_HALF_UP, which rounds 0.5 away from zero.
>>> from decimal import Decimal, ROUND_HALF_UP
>>> Decimal("2.675").quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
Decimal('2.68')
>>> Decimal("-2.5").quantize(Decimal("1"), rounding=ROUND_HALF_UP)
Decimal('-3')
quantize() takes a Decimal with the number of places you want to round to. For two decimal places that’s Decimal("0.01"); for an integer it’s Decimal("1").
The key point is to create the Decimal from a string. If you pass a float directly, the error from the previous section comes along with it, and specifying ROUND_HALF_UP won’t change the result.
>>> Decimal(2.675).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
Decimal('2.67')
>>> Decimal(str(2.675)).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
Decimal('2.68')
str(2.675) returns '2.675' because, when Python converts a float to a string, it picks the shortest decimal representation that round-trips to the original value. If you’ll be rounding values repeatedly, a function like this makes things easier.
from decimal import Decimal, ROUND_HALF_UP
def round_half_up(value, digits=0):
"""Round value half up to digits decimal places (digits >= 0)"""
exp = Decimal(1).scaleb(-digits) # Decimal('0.01') when digits=2
return Decimal(str(value)).quantize(exp, rounding=ROUND_HALF_UP)
for v, d in [(2.5, 0), (-2.5, 0), (2.675, 2), (1.005, 2), ("0.125", 2)]:
print(f"{v!s:>7} → {round_half_up(v, d)}")
2.5 → 3
-2.5 → -3
2.675 → 2.68
1.005 → 1.01
0.125 → 0.13
This function returns a Decimal; do any display formatting or conversion with int() or float() afterward. If you pass a negative digits to round to the tens place, the result becomes a Decimal in exponent notation such as 1.23E+3, so for that case it’s better to write it separately, for example by converting the result of quantize(Decimal("1E1"), ...) with int().
Keep in mind that str() only rescues the case where “a 2.675 typed by a person” has simply been turned into a float. For a value where error has built up during calculation, such as 1.1 * 3, str() still gives 3.3000000000000003. For values that can’t tolerate error, like money, the safe approach is to turn them into Decimal straight from the string when you read them from a CSV file or form, and to do the arithmetic between Decimal values too.
>>> price = Decimal("1125")
>>> (price * Decimal("0.1")).quantize(Decimal("1"), rounding=ROUND_HALF_UP)
Decimal('113')
The default rounding for Decimal arithmetic itself is round half to even (ROUND_HALF_EVEN), the same as round(). You can check it with decimal.getcontext().rounding. Wherever you need half-up rounding, state the mode explicitly with quantize() as above. Passing a Decimal to round() also rounds half to even: round(Decimal("2.5")) returns 2.
4. Common pitfalls in hand-rolled rounding
A frequently suggested alternative that avoids decimal is to add 0.5 and truncate.
import math
def round_half_up_float(x, digits=0):
p = 10 ** digits
return math.floor(x * p + 0.5) / p
This approach has two weaknesses.
The first is that multiplication introduces error too. Multiplying 1.005 by 100 doesn’t give 100.5.
>>> 1.005 * 100
100.49999999999999
>>> round_half_up_float(1.005, 2)
1.0
With 2.675, the multiplication error happens to go upward, giving 267.5, so the function returns 2.68. Because it’s right for some values and wrong for others, a handful of tests can easily miss the problem.
The second is that a version using int() instead of math.floor() gets negative numbers wrong. int() truncates toward zero.
>>> int(-2.5 + 0.5)
-2
>>> int(-2.6 + 0.5)
-2
Rounding -2.6 should give -3, but -2.1 is truncated toward zero and you get -2. With math.floor(), -2.6 does become -3, but now -2.5 becomes -2, which doesn’t match half-up rounding away from zero. In code that handles negative amounts, such as returns or discounts, this difference goes straight into the totals.
5. Watch negative numbers when truncating or rounding up, too
Other kinds of rounding also behave differently from function to function once negative numbers are involved.
| What you want | With float | 2.7 | -2.7 |
|---|---|---|---|
| Truncate toward zero | int(x), math.trunc(x) |
2 | -2 |
| Round down (toward smaller) | math.floor(x) |
2 | -3 |
| Round up (toward larger) | math.ceil(x) |
3 | -2 |
To do the same with Decimal, pass ROUND_DOWN (toward zero), ROUND_FLOOR (toward smaller), ROUND_CEILING (toward larger) or ROUND_UP (away from zero) as the rounding argument to quantize(). When a spec says “truncate”, it often doesn’t say which of these it means, because it was written with only positive numbers in mind. If negative values might come in, it’s worth pinning this down when the spec is being written.
6. What happens in JavaScript and spreadsheets
The discrepancy at the start of this article came from Python and spreadsheets rounding differently. I rounded the same values in other environments as well. JavaScript was run on Node.js v22.23.5, the spreadsheet was LibreOffice Calc 24.2.7.2, and NumPy 2.5.3 and pandas 3.0.5 were run on Python 3.13.
| Environment / function | 2.5 | -2.5 | 2.675 (2 places) | 1.005 (2 places) | 0.125 (2 places) |
|---|---|---|---|---|---|
Python round() |
2 | -2 | 2.67 | 1.0 | 0.12 |
NumPy np.round(), pandas .round() |
2.0 | – | – | – | – |
Python Decimal + ROUND_HALF_UP (from a string) |
3 | -3 | 2.68 | 1.01 | 0.13 |
JavaScript Math.round() |
3 | -2 | – | – | – |
JavaScript toFixed() |
“3” | “-3” | “2.67” | “1.00” | “0.13” |
JavaScript Intl.NumberFormat |
3 | -3 | – | – | – |
LibreOffice Calc ROUND() |
3 | -3 | 2.68 | 1.01 | 0.13 |
“–” marks combinations I didn’t test this time. NumPy and pandas rounded [0.5, 1.5, 2.5] to [0, 2, 2], rounding half to even just like Python’s round().
JavaScript’s Math.round() rounds midpoint values toward the larger number. 2.5 becomes 3, but -2.5 becomes -2, not -3. toFixed() works from the float’s actual value, so 2.675 and 1.005 round down just as in Python. On the other hand, 0.125, which can be represented exactly, becomes 0.13, so here the result differs from Python’s.
LibreOffice Calc’s ROUND() rounded both 2.675 and 1.005 “as they appear”, to 2.68 and 1.01. For Excel’s ROUND(), Microsoft’s help gives examples where =ROUND(2.15, 1) returns 2.2 and =ROUND(-1.475, 2) returns -1.48 (ROUND function). Python’s round(2.15, 1) is 2.1, so the results disagree here as well. When moving a spreadsheet calculation to Python, replacing the rounding steps with Decimal and ROUND_HALF_UP makes it easier to get matching results.
Another common problem when reading data exported from Excel into Python is dates turning into numbers. That’s covered in Why Excel dates turn into numbers (serial values) (in Japanese).
7. Which rounding method to use
The choice isn’t about which method is “correct” but about how the number will be used.
- Amounts on invoices and reports: follow whatever rounding rule (half up, truncation and so on) is set by internal policy or agreed with the other party. Handle values as
Decimalfrom the moment they’re read through every calculation, and state the rounding mode explicitly wherever you round. - Measurements and statistical aggregates: if you round a large number of values and then sum or average them, round half to even (
round()) introduces less bias. Better still is not to round along the way at all, and to round only for the final display. - Values that are only displayed on screen:
f"{x:.2f}"is often enough. There’s no guarantee which way it rounds when the last digit is 5, though, so don’t reuse the displayed value in other calculations.
In cases like how many decimal places to keep for latitude and longitude, where errors on the order of meters from rounding are acceptable, round() is rarely a problem. Trouble starts when you have to reconcile figures with another system down to a single yen or a single record. If you come across round() in that kind of code, it’s worth feeding it values ending in exactly 5 and negative numbers once to confirm it gives the results you expect.
Try it yourself
Code to reproduce the results in this article (Python 3, standard library only)
Save the following code as rounding_check.py and run it with python3 rounding_check.py. It lets you check the Python parts of the tables and the consumption tax calculation.
import math
from decimal import Decimal, ROUND_HALF_UP
print([round(x) for x in (0.5, 1.5, 2.5, 3.5, -0.5, -1.5, -2.5)])
print(round(2.675, 2), round(1.005, 2), round(0.125, 2), round(0.375, 2))
print(Decimal(2.675))
print(1.005 * 100, int(-2.6 + 0.5))
def half_up(s, exp="1"):
return Decimal(s).quantize(Decimal(exp), rounding=ROUND_HALF_UP)
print(half_up("2.675", "0.01"), half_up("1.005", "0.01"), half_up("-2.5"))
total_round = total_half_up = 0
for price in range(1125, 1200, 10):
tax = Decimal(price) * Decimal("0.1")
total_round += round(price * 0.1)
total_half_up += int(half_up(tax))
print(total_round, total_half_up) # 928 932
You can check the JavaScript results in the console of your browser’s developer tools.
[2.5, -2.5].map(Math.round); // [3, -2]
[(2.675).toFixed(2), (1.005).toFixed(2), (0.125).toFixed(2)];
// ['2.67', '1.00', '0.13']