Full answer
sql
SELECT ROUND(2.5) AS pos, ROUND(-2.5) AS neg, ROUND(3.5) AS pos2, typeof(ROUND(2.5)) AS t;| pos | neg | pos2 | t |
|---|---|---|---|
| 3.0 | -3.0 | 4.0 | real |
SQLite returns a real (3.0), not an integer.
sql
SELECT ROUND(2.5) AS pos, ROUND(-2.5) AS neg, ROUND(3.5) AS pos2;| pos | neg | pos2 |
|---|---|---|
| 3 | -3 | 4 |
PostgreSQL treats 2.5 as numeric, so halves go away from zero. Cast to double precision and the result changes:
sql
SELECT ROUND(2.5::float8) AS pos, ROUND(-2.5::float8) AS neg, ROUND(3.5::float8) AS pos2;| pos | neg | pos2 |
|---|---|---|
| 2.0 | -2.0 | 4.0 |
Banker's rounding (half to even) avoids upward bias when rounding many values, which matters in finance and statistics. When rounding rules matter, use exact types, apply the rule explicitly, and test the halfway cases.
sql
SELECT ROUND(2.5) AS pos, ROUND(-2.5) AS neg, ROUND(3.5) AS pos2, typeof(ROUND(2.5)) AS t;Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.
Key points
- SQLite rounds halves away from zero and returns a real
- PostgreSQL numeric rounds halves away from zero
- PostgreSQL double precision rounds half to even
- The argument type decides the rounding rule
Common mistakes
- Assuming every engine and type rounds 2.5 to 3
- Rounding money stored as floats
Follow-ups
- What does ROUND(1.005, 2) return with floats?
- When is banker's rounding preferred?
Tips
- Test halfway values explicitly