Skip to content

sql_func_round

SQL ROUND() Function: Controlling Numeric Precision

Section titled “SQL ROUND() Function: Controlling Numeric Precision”

The ROUND() function in SQL is used to round a numeric value to a specified number of decimal places or to the nearest integer. This is crucial for data presentation, financial calculations, and ensuring consistency in numerical data.

Understanding Rounding Behavior (Important Note)

Section titled “Understanding Rounding Behavior (Important Note)”

It’s important to be aware that different database systems and configurations might handle rounding, especially for numbers ending in ‘.5’ (midpoint values), in various ways. A common behavior, often aligned with the IEEE 754 standard, is “round half to even” (also known as “banker’s rounding”). In this scheme:

  • Numbers like 11.5 are rounded to the nearest even integer, so ROUND(11.5, 0) might become 12.
  • Numbers like 12.5 are also rounded to the nearest even integer, so ROUND(12.5, 0) might also become 12.
  • Traditional school rounding (round .5 up) is not always the default. For example, ROUND(11.5, 0) would be 12, and ROUND(12.5, 0) would be 13.
  • Always test ROUND() behavior in your specific DBMS if precise midpoint rounding is critical.
SELECT ROUND(numeric_column_or_value, number_of_decimals) FROM table_name;

Or, when rounding a literal value:

SELECT ROUND(numeric_value, number_of_decimals);
ParameterDescription
numeric_column_or_valueRequired. The numeric field or literal value to be rounded. This can be a column name of a numeric type (e.g., DECIMAL, FLOAT, DOUBLE) or a direct number.
number_of_decimalsRequired. An integer specifying the number of decimal places to round to.
- If positive, rounds to that many decimal places (e.g., 2 for xx.yy).
- If zero, rounds to the nearest whole integer (e.g., xx).
- If negative (supported by some DBMS like Oracle, PostgreSQL), rounds to the left of the decimal point (e.g., -1 rounds to the nearest 10, -2 to the nearest 100).

Let’s use a simplified Products table, similar to what you might find in the Northwind sample database, for our examples.

Sample Products table data:

ProductID | ProductName | UnitPrice
----------|-----------------------------|-----------
1 | Chai | 18.00
2 | Chang | 19.00
3 | Aniseed Syrup | 10.00
4 | Chef Anton's Cajun Seasoning| 22.357
5 | Chef Anton's Gumbo Mix | 21.35
6 | Grandma's Boysenberry Spread| 25.495

Example 1: Round product prices to the nearest whole number.

SELECT ProductName, UnitPrice, ROUND(UnitPrice, 0) AS RoundedToInteger
FROM Products;

Result might look like (depending on specific rounding rules for .5):

ProductName | UnitPrice | RoundedToInteger
-----------------------------|-----------|-----------------
Chai | 18.00 | 18
Chang | 19.00 | 19
Aniseed Syrup | 10.00 | 10
Chef Anton's Cajun Seasoning| 22.357 | 22
Chef Anton's Gumbo Mix | 21.35 | 21
Grandma's Boysenberry Spread| 25.495 | 25 or 26 (depending on .5 handling)

Example 2: Round product prices to one decimal place.

SELECT ProductName, UnitPrice, ROUND(UnitPrice, 1) AS RoundedToOneDecimal
FROM Products;

Result might look like:

ProductName | UnitPrice | RoundedToOneDecimal
-----------------------------|-----------|--------------------
Chai | 18.00 | 18.0
Chang | 19.00 | 19.0
Aniseed Syrup | 10.00 | 10.0
Chef Anton's Cajun Seasoning| 22.357 | 22.4
Chef Anton's Gumbo Mix | 21.35 | 21.4
Grandma's Boysenberry Spread| 25.495 | 25.5

Example 3: Rounding to the nearest ten (if supported by your DBMS, using a negative decimal count).

-- This syntax for negative decimals is common in Oracle, PostgreSQL.
-- MySQL's ROUND() might behave differently or require TRUNCATE for this effect.
SELECT ProductName, UnitPrice, ROUND(UnitPrice, -1) AS RoundedToNearestTen
FROM Products WHERE ProductID IN (4,6);

Result (example for PostgreSQL/Oracle):

ProductName | UnitPrice | RoundedToNearestTen
-----------------------------|-----------|--------------------
Chef Anton's Cajun Seasoning| 22.357 | 20
Grandma's Boysenberry Spread| 25.495 | 30
  • Financial Reporting: Displaying currency values with a standard two decimal places.
  • Data Cleaning: Standardizing precision of numerical data imported from various sources.
  • User Interface Display: Presenting numbers in a more user-friendly and less cluttered format.
  • Statistical Calculations: Adjusting precision before further analysis.

SQL offers other functions for numerical adjustments:

  • FLOOR(numeric_value): Returns the largest integer value less than or equal to numeric_value (rounds down).
  • CEILING(numeric_value) or CEIL(numeric_value): Returns the smallest integer value greater than or equal to numeric_value (rounds up).
  • TRUNC(numeric_value, [decimals]) or TRUNCATE(numeric_value, decimals): Truncates the number to a specified number of decimal places without rounding. Behavior with negative numbers or the second argument can vary by DBMS.

Always consult your specific DBMS documentation for the precise behavior of these functions, especially concerning rounding rules and parameter support.