Back to Numeric Functions

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`.

idlocationtemperature
1New York25.678
2Los Angeles30.123
3Chicago20.987
4Miami35.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:

idlocationtemperaturerounded_temperature
1New York25.67825.68
2Los Angeles30.12330.12
3Chicago20.98720.99
4Miami35.54335.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.

Aspirant's Home is an innovative educational and career development platform designed to empower individuals in achieving their academic and professional aspirations.
Terms of Use   About Us   Privacy Policy