PostgreSQL ROUND Function: How to Use It
In this article
The PostgreSQL ROUND function rounds a numeric value to a specified number of decimal places. For example, SELECT ROUND(3.14159, 2); returns 3.14. It works on numeric and double precision types, but the optional second argument (decimal places) only applies to numeric; for double precision you must use one argument, which rounds to the nearest integer. This guide shows you how to use it correctly, avoid common pitfalls, and troubleshoot unexpected results.
What You'll Need
- A PostgreSQL database (any recent version) and a client such as psql, pgAdmin, or any SQL tool.
- Basic familiarity with SQL SELECT statements.
- Permission to run queries in your database.
Step-by-Step
- Connect to your database. Open psql or your SQL client and connect to the database where you want to run queries.
- Round to the nearest integer. Run
SELECT ROUND(3.7);— it returns4. For a negative number,SELECT ROUND(-3.7);returns-4(rounds away from zero). - Round to a specific number of decimal places. Use two arguments:
SELECT ROUND(3.14159, 2);returns3.14. The second argument must be an integer; it can be negative to round left of the decimal point, e.g.,SELECT ROUND(1234.56, -2);returns1200. - Apply ROUND to a column. Suppose you have a table
saleswith a columnamountof type numeric. RunSELECT ROUND(amount, 2) FROM sales;to display each amount rounded to two decimals. The original data is not changed; ROUND only affects the query output. - Use ROUND in calculations. You can round the result of an expression:
SELECT ROUND(price * 1.07, 2) AS total FROM products;. - Handle double precision correctly. If the column is
double precision(orreal), the two-argument form is not available. UseROUND(value)to round to the nearest integer, or cast to numeric first:SELECT ROUND(value::numeric, 2) FROM measurements;. Casting is the standard workaround. - Round in an UPDATE (if needed). To permanently round stored values, use
UPDATE sales SET amount = ROUND(amount, 2);. This modifies data — back up or test on a copy first. - Check the result type. ROUND on numeric returns numeric; on double precision it returns double precision. If you need a specific type, cast the result:
SELECT ROUND(3.14159, 2)::float8;.
Common Mistakes
- Using two arguments with double precision.
ROUND(3.14159::double precision, 2)raises an error: function round(double precision, integer) does not exist. Cast to numeric first. - Expecting ROUND to change stored data. ROUND only affects the query result. To change data, use UPDATE.
- Confusing rounding with truncation. ROUND(2.5) returns 3, while TRUNC(2.5) returns 2. Use TRUNC if you need to cut off digits.
- Assuming bankers' rounding. PostgreSQL rounds half away from zero for numeric, not to the nearest even number. So ROUND(2.5) = 3, ROUND(3.5) = 4.
- Forgetting negative precision. A negative second argument rounds to the left of the decimal (e.g., to the nearest hundred). This is valid but often overlooked.
Tips
- Use
ROUNDfor display formatting; useTRUNCfor truncation,CEILfor rounding up, andFLOORfor rounding down. - When working with money, store values as
numeric(notdouble precision) to avoid floating-point errors and to use two-argument ROUND directly. - To round to a fixed number of decimals for output, consider
TO_CHARfor formatting:SELECT TO_CHAR(3.14159, 'FM999.00');returns '3.14'. - Test rounding behavior with sample values, especially negative numbers and halfway cases, to confirm it matches your expectations.
Troubleshooting
- Error: function round(double precision, integer) does not exist. The column is double precision. Cast to numeric:
ROUND(column::numeric, 2). - Result has more decimals than expected. Check the data type. If it is numeric, the result should have the specified scale. If you see trailing zeros, that is normal for numeric; use
::float8orTO_CHARto format. - Rounding seems inconsistent. Remember halfway values round away from zero. For example, ROUND(2.5) = 3, ROUND(-2.5) = -3. If you need different behavior, implement custom logic.
- UPDATE did not change data. Verify the column type and that the WHERE clause matched rows. Also check for triggers or constraints that might revert changes.
- Performance concerns. ROUND is cheap, but applying it to every row in a large table can add overhead. Use it only when needed, and avoid it in WHERE clauses if it prevents index usage.
Frequently Asked Questions
Can I use ROUND with a negative number of decimal places?
Yes, for numeric types. For example, ROUND(1234.56, -2) returns 1200. This rounds to the nearest hundred.
Does ROUND work on integer columns?
Yes, but it is redundant because integers have no decimal part. ROUND(5) returns 5.
What is the difference between ROUND and TRUNC?
ROUND rounds to the nearest value, while TRUNC simply cuts off digits without rounding. For example, ROUND(2.9) = 3, TRUNC(2.9) = 2.
How do I round a double precision column to two decimals?
Cast it to numeric first: ROUND(column::numeric, 2). The two-argument form is not defined for double precision.
Does ROUND modify the original data?
No, ROUND only affects the query output. To change stored data, use an UPDATE statement.
Can I use ROUND in a WHERE clause?
Yes, but be aware that applying a function to a column in a WHERE clause can prevent the use of an index, potentially slowing queries. Consider rounding in the SELECT list instead.