The ROUND function rounds off a value to a specified number of decimal places. Fractions greater than or equal to .5 are rounded up. Fractions less than .5 are rounded down (truncated).
Syntax
ROUND ( expression [ , decimal-places ] )
Semantics
expression |
is an expression of type DOUBLE PRECISION |
decimal-places |
if positive, specifies the number of decimal places to display to the right of the decimal point; if negative, specifies the number of decimal places to display to the left of the decimal point. |
Notes
> SELECT ROUND(3.499999999999999); -- 15 decimal places
round
-------
3
(1 row)
The internal integer representation used to compute the ROUND function causes the fraction to be evaluated precisely and it is thus rounded down. However:
> SELECT ROUND(3.4999999999999999); -- 16 decimal places
round
-------
4
(1 row)
The internal floating point representation used to compute the ROUND function causes the fraction to be evaluated as 3.5, which is rounded up.
Examples
SELECT ROUND(3.14159, 3);
3.142
SELECT ROUND(1234567, -3);
1235000
SELECT ROUND(3.4999, -1);
0