Numbers

ROUND

Rounds a number to an integer or to the number of decimal places you need.

Syntax

ROUND(<number>)
ROUND(<number>, <decimals>)

Without the second argument it rounds to an integer; with it — to the given number of digits after the decimal point. A negative number of places rounds to the left of the point: to tens, hundreds, thousands.

Data for the examples

orders

idcreated_atamount
12026-07-03 10:15:00120.50
22026-07-28 18:40:0080.25
32026-08-09 09:05:00199.99
42026-08-21 14:30:0045.00
52026-09-02 11:00:0064.75

These tables already exist in the sandbox — the examples below use them. Want to try this in your own database? Copy the SQL.

To hundredths, to an integer and to hundreds
SELECT ROUND(19.857, 2) AS two_decimals,
ROUND(19.857) AS to_integer,
ROUND(1234.5, -2) AS to_hundreds;
two_decimalsto_integerto_hundreds
19.86201200
The average order value with two decimals
SELECT AVG(amount) AS average,
ROUND(AVG(amount), 2) AS rounded
FROM orders;
averagerounded
102.0980000000000000102.10
One half — two different results
SELECT ROUND(2.5) AS numeric_half,
ROUND(2.5::float8) AS float_half,
ROUND(3.5::float8) AS float_half_2;
numeric_halffloat_halffloat_half_2
324

For numeric, a half rounds away from zero; for double precision, to the nearest even number.

Watch out

ROUND(x, 2) exists only for the numeric type. If x is double precision (say, an average of a real column), you'll get function round(double precision, integer) does not exist. The fix is to cast: ROUND(x::numeric, 2).

For double precision, halves round to the nearest even number: ROUND(2.5::float8) gives 2, and ROUND(3.5::float8) gives 4. Use numeric for money.

Related functions

Learn more in the course