Full answer
sql
SELECT ROUND(123.456, 1) AS one_dp, ROUND(123.456, -1) AS neg_precision, ROUND(123.456 / 10.0) * 10 AS to_tens;| one_dp | neg_precision | to_tens |
|---|---|---|
| 123.5 | 123.0 | 120.0 |
PostgreSQL:
sql
SELECT ROUND(123.456, 1) AS one_dp, ROUND(123.456, -1) AS neg_precision;| one_dp | neg_precision |
|---|---|
| 123.5 | 120 |
Negative precision is handy for report bucketing ("revenue to the nearest thousand"), but it is not portable. The divide, round and multiply form works on both engines. SQLite returns a real (120.0), while PostgreSQL's numeric ROUND returns 120 without a fractional part. If the value is shown to users, format it explicitly rather than relying on how each engine prints numbers.
sql
SELECT ROUND(123.456, 1) AS one_dp, ROUND(123.456, -1) AS neg_precision, ROUND(123.456 / 10.0) * 10 AS to_tens;Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.
Key points
- ROUND(x, n) rounds to n decimal places
- PostgreSQL accepts negative n to round to tens, hundreds
- SQLite treats negative n as 0
- ROUND(x / 10.0) * 10 is portable
Common mistakes
- Relying on negative precision in SQLite
- Rounding money stored as floats
Follow-ups
- How would you round to the nearest 5?
- What does TRUNC do differently?
Tips
- Give the portable form