PostgreSQL round() Function
The PostgreSQL round() function rounds the specified number to the specified precision and returns the result.
round() Syntax
This is the syntax of the PostgreSQL round() function:
round(numeric_value) -> integer
or
round(numeric_value, scale) -> numeric
Parameters
numeric_value-
Required. The number to round, it can be positive, negative, or zero, and it can be an integer or a decimal.
scale-
Optional. An integer representing numeric precision. Default is
0.
Return value
The PostgreSQL round() function returns an numeric after rounding the specified number with specified precision.
The round() function will return NULL if the argument is NULL.
PostgreSQL will give an error if you supply a parameter that is not a numeric type.
round() Examples
The following example demonstrates how to use the round() function to round a decimal to an integer.
SELECT
round(10.11) AS "round(10.11)",
round(10.51) AS "round(10.51)",
round(-10.11) AS "round(-10.11)",
round(-10.51) AS "round(-10.51)";
round(10.11) | round(10.51) | round(-10.11) | round(-10.51)
--------------+--------------+---------------+---------------
10 | 11 | -10 | -11The following example shows how to use the round() function to round a decimal with 2 decimal digits in the fractional part.
SELECT
round(10.1212, 2) AS "round(10.1212, 2)",
round(10.5151, 2) AS "round(10.5151, 2)",
round(-10.1212, 2) AS "round(-10.1212, 2)",
round(-10.5151, 2) AS "round(-10.5151, 2)";
round(10.1212, 2) | round(10.5151, 2) | round(-10.1212, 2) | round(-10.5151, 2)
-------------------+-------------------+--------------------+--------------------
10.12 | 10.52 | -10.12 | -10.52