MySQL ROUND() Function: Rounding Numbers in MySQL
Learn how to use the MySQL `ROUND()` function to round numbers to a specific number of decimal places or to the nearest integer. This guide explains the function's syntax, provides practical examples, and demonstrates how to control the precision of numeric values within your MySQL queries.
MySQL ROUND() Function
Definition and Usage
The ROUND()
function in MySQL rounds a number to a specified number of decimal places. If you omit the number of decimal places, it rounds to the nearest integer.
Syntax
Syntax
ROUND(number, decimals)
Parameter Values
Parameter | Description |
---|---|
number |
Required. The number to be rounded. |
decimals |
Optional. The number of decimal places to round to. If omitted, the number is rounded to the nearest integer. |
Examples
Example 1: Rounding to Two Decimal Places
This example rounds the number 135.375 to two decimal places.
SQL Query
SELECT ROUND(135.375, 2);
Output
ROUND(135.375, 2)
-----------------
135.38
Example 2: Rounding to Zero Decimal Places (Nearest Integer)
This example rounds 345.156 to the nearest integer (zero decimal places).
SQL Query
SELECT ROUND(345.156, 0);
Output
ROUND(345.156, 0)
-----------------
345
Example 3: Rounding a Column's Values
This example rounds the "Price" column in a "Products" table to one decimal place.
SQL Query
SELECT ProductName, Price, ROUND(Price, 1) AS RoundedPrice FROM Products;
Output
-- Output will vary depending on your Products table. Example:
-- ProductName | Price | RoundedPrice
-- ------------- | -------- | -------------
-- Product A | 12.95 | 13.0
-- Product B | 25.5 | 25.5
-- Product C | 100.02 | 100.0
-- ...and so on...
Technical Details
The ROUND()
function is available in MySQL 4.0 and later.
For other rounding and truncation functions, see FLOOR()
, CEIL()
, CEILING()
, and TRUNCATE()
.