PostgreSQL - How to round column values to some decimal places?
How to round column values to some decimal places?
The version of round that takes a precision is only available for numeric
, so you have to cast the value to be rounded to numeric
.
1. For column values
To round values of a whole column to n
decimal places:
SELECT
ROUND(column_name::numeric, n)
FROM
table_name;
Or using the CAST() function:
SELECT
ROUND(CAST(column_name AS NUMERIC), n)
FROM
table_name;
For example, you want to round the column amount
in the table payment to 2 decimal places:
SELECT
round(CAST(amount AS NUMERIC), 2) AS rounded_amount
FROM
payment;
2. For a specific value
To round a specific value:
SELECT
round(CAST(number_value AS NUMERIC), n);
For example:
SELECT
round(CAST(0294.124898 AS NUMERIC), 3);
The result is 294.125
Need a good GUI tool for PostgreSQL? Check out TablePlus. It’s native, beautiful, and available for free.
Not on Mac? Download TablePlus for Windows.
On Linux? Download TablePlus for Linux
Need a quick edit on the go? Download TablePlus for iOS