
Mysql - ROUND(x, d) Function
The `ROUND()` function in MySQL is used to round a numeric value to a specified number of decimal places. It follows standard rounding rules: if the fractional part is less than 0.5, it rounds down; if it's 0.5 or greater, it rounds up.
Here is a simple example using the `ROUND()` function:
SELECT ROUND(4.7);
This query will return `5`, as 4.7 rounds up to 5.
Rounding to Decimal Places
You can also specify the number of decimal places to round to:
SELECT ROUND(4.5678, 2);
This query will return `4.57`, rounding 4.5678 to 2 decimal places.
ROUND(x, d) with MySQL Table
Consider a table `temperatures` with columns `location` and `temperature`.
id | location | temperature |
---|---|---|
1 | New York | 25.678 |
2 | Los Angeles | 30.123 |
3 | Chicago | 20.987 |
4 | Miami | 35.543 |
If you want to round the `temperature` of each location to two decimal places, you can use the ROUND function as follows:
SELECT location, temperature, ROUND(temperature, 2) AS rounded_temperature FROM temperatures;
This query will return:
id | location | temperature | rounded_temperature |
---|---|---|---|
1 | New York | 25.678 | 25.68 |
2 | Los Angeles | 30.123 | 30.12 |
3 | Chicago | 20.987 | 20.99 |
4 | Miami | 35.543 | 35.54 |
In this example, the ROUND function is used to round the `temperature` column to two decimal places. The `ROUND(temperature, 2)` expression returns the rounded value for each row.

