SQL banker's rounding
Banker's rounding sends ties to the even neighbour: 2.5 to 2, 3.5 to 4. Three engines apply it to float input; none applies it to exact decimals.
- Categoría
- Math
- Returns
numeric- In this category
- 15 of 82
Sintaxis
ROUND(2.5) -- half-up, 3
round-to-even, 2
Each block is a separate form. The support table and the examples show which engine takes which.
Soporte del motor
| Motor | Support | Notas |
|---|---|---|
| SQL Server | Not built in | ROUND is half away from zero for decimal and float alike; emulate with CASE |
| PostgreSQL | Different name or syntax | round(double precision) rounds ties to even; round(numeric) does not |
| MySQL | Different name or syntax | ROUND on DOUBLE rounds ties to even (ROUND(2.5e0) is 2); DECIMAL rounds half away from zero |
| SQLite | Not built in | round() is half away from zero; no built-in banker's rounding |
| Oracle | Different name or syntax | ROUND on BINARY_DOUBLE/BINARY_FLOAT rounds ties to even; NUMBER does not |
Ejemplos
PostgreSQL
SELECT ROUND(2.5::float8) AS f, ROUND(2.5::numeric) AS n; -- 2, 3
MySQL
SELECT ROUND(2.5e0) AS dbl, ROUND(2.5) AS dec_value; -- 2, 3
Oracle
SELECT ROUND(2.5d) AS binary_dbl, ROUND(2.5) AS num FROM dual; -- 2, 3
MySQL
SELECT CASE WHEN x - FLOOR(x) = 0.5 AND MOD(FLOOR(x), 2) = 0 THEN FLOOR(x) ELSE ROUND(x) END AS banker FROM t; -- 2.5 gives 2, 3.5 gives 4 (x >= 0)
Conviene saberlo
- Only exact ties are affected, and a float tie is rarer than it looks: 2.5 stores exactly, but 0.285 * 100 computed in doubles is 28.499999999999996, so ROUND sees no tie at all and returns 28.
- Casting changes the rule silently: ROUND(x) on a DOUBLE column gives 2 for 2.5, ROUND(CAST(x AS DECIMAL(10,2))) gives 3. Mixed-type reports can disagree by one unit at every tie.
- To emulate on exact decimals, test for the tie and pick the even neighbour: CASE WHEN x - FLOOR(x) = 0.5 AND MOD(FLOOR(x), 2) = 0 THEN FLOOR(x) ELSE ROUND(x) END covers non-negative x at 0 decimals.
Questions about ROUND half-even
Does any SQL ROUND use banker's rounding by default?
Yes, on approximate floats: PostgreSQL round(double precision), MySQL ROUND on DOUBLE and Oracle ROUND on BINARY_DOUBLE send ties to even. SQL Server and SQLite never do. Exact decimal types (NUMERIC, DECIMAL, NUMBER) round ties away from zero on all five engines.
Why do floats round 2.5 down to 2?
2.5 is stored exactly, so this is not a storage error: those engines implement float ROUND with the C library's rint-style ties-to-even rule. PostgreSQL shows both side by side: round(2.5::float8) is 2, round(2.5::numeric) is 3.
How do I force banker's rounding on exact decimals?
No engine ships it for NUMERIC/DECIMAL. Emulate it with a CASE: when the value sits exactly on the half (x - FLOOR(x) = 0.5) and FLOOR(x) is even, return FLOOR(x); otherwise ROUND(x). Scale by 10^d first to round at d decimals.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: PostgreSQL, MySQL, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.