← Back to SQL, page 16

sql-0313 · Medium mid

What does ROUND(123.456, -1) return, and what does ROUND(123.456, 1) return?

ROUND(123.456, 1) is 123.5 on both engines. ROUND(123.456, -1) is 120 in PostgreSQL, rounding to the nearest ten, while SQLite treats a negative precision as 0 and returns 123.0. Use ROUND(x / 10.0) * 10 for a portable round-to-tens.

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_dpneg_precisionto_tens
123.5123.0120.0

PostgreSQL:

sql
SELECT ROUND(123.456, 1) AS one_dp, ROUND(123.456, -1) AS neg_precision;
one_dpneg_precision
123.5120

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