// math reference

SQL ROUND half-up rounding

Documents that every engine's ROUND rounds ties away from zero for exact decimal types (2.5 to 3, -2.5 to -3) and which inputs break that.

カテゴリ
Math
Returns
numeric
In this category
14 of 82

構文

ROUND(2.5, 0)
ROUND(-2.5, 0)

Each block is a separate form. The support table and the examples show which engine takes which.

エンジンサポート

エンジンSupportメモ
SQL Server はい Supported under this name.
PostgreSQL はい True for numeric input: round(2.5::numeric) is 3. For double precision, round() follows IEEE ties-to-even instead
MySQL はい Half away from zero for exact-value (DECIMAL) arguments; DOUBLE input rounds ties to even, so ROUND(2.5e0) is 2
SQLite はい round(2.5) is 3.0 and round(-2.5) is -3.0
Oracle はい NUMBER ROUND is half away from zero; BINARY_DOUBLE follows IEEE

例

Any engine

SELECT ROUND(2.5, 0) AS up, ROUND(3.5, 0) AS also_up;  -- 3, 4

Any engine

SELECT ROUND(-2.5, 0) AS neg;  -- -3 (away from zero)

PostgreSQL

SELECT ROUND(2.5::numeric) AS numeric_round, ROUND(2.5::float8) AS float_round;  -- 3, 2

知っておくと便利なこと

  • Half-up on stored floats is a coin flip: the literal 2.5 stores exactly, but 2.675 does not, so ROUND(2.675, 2) can give 2.67.
  • Negative values go away from zero, not up: ROUND(-2.5) is -3, not -2.
  • The type decides the tie rule on three engines: PostgreSQL double precision, MySQL DOUBLE and Oracle BINARY_DOUBLE round ties to even, so ROUND(2.5e0) is 2 on MySQL. SQL Server rounds float ties away from zero too.

Questions about ROUND half-up

Does SQL round 2.5 up to 3?

Yes on every engine when the value is an exact decimal: ROUND(2.5, 0) is 3 and ROUND(3.5, 0) is 4 (half away from zero). SQL Server requires the second argument; the other four accept ROUND(2.5). The exception is float input on PostgreSQL, MySQL and Oracle, where ties go to the even neighbour and 2.5 becomes 2.

Is half-up the same as half-away-from-zero?

For positive numbers yes. For negatives they differ: half-up (toward positive infinity) would send -2.5 to -2, while every engine's ROUND sends it to -3. The engines implement half away from zero.

Why did my tie round the wrong way?

Usually the value is not an exact tie. 2.675 stored as a double sits just below the tie, so ROUND(2.675, 2) gives 2.67. Keep tie-sensitive data in DECIMAL/NUMERIC.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.

// fewer lookups

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.