Round MySQL
Overview
One of the most popular relational database management systems available for free is MySQL. To round a value to a given number of decimal places, use MySQL's ROUND() function. This function is often used in financial calculations, where precise values are required.
Syntax of MySQL ROUND() Function
The syntax for the MySQL ROUND() function is as follows:
The number parameter specifies the number to be rounded. decimal_places is an optional parameter; it specifies the number of decimal places to which the number should be rounded.
Parameters of MySQL ROUND() Function
The ROUND() function takes two parameters:
-
Number
- The number to be rounded.
- It can be a numeric value, a column that contains numeric values, or an expression that evaluates to a numeric value.
-
Decimal_places
- The number of decimal places to which the number should be rounded.
- It is an optional parameter, and if it is omitted, the ROUND() function rounds the number to the nearest integer.
Return Value of MySQL ROUND() Function
The ROUND() function returns a numeric value that is rounded to the specified number of decimal places.
How does the MySQL ROUND() Function work?
When using the ROUND() function, a number is rounded to the desired number of decimal points. The function rounds the value to the nearest integer if the decimal places parameter is omitted. The function rounds the value to the specified number of decimal places if the decimal places input is a positive integer. If the decimal places parameter is negative, the function rounds the value to the left of the decimal point.
Examples
Let's take a look at some examples of how to use the MySQL ROUND() function:
1) Round a number to the nearest integer
Code
Output
When the decimal_place parameter is omitted then the Round() function rounds the number to the nearest integer.
2) Round a number to a specific number of decimal places
Code
Output
When the decimal_place parameter is entered, it rounds the number to that number of decimal places
3) Round a number to a negative decimal place:
a) When decimal_places is positive
Code
Output
The number parameter can take positive as well as negative numbers.
b) When decimal_places is negative
Code
Output
The query above returns 1800 because the second digit before the decimal point (which is to be deleted because of decimal_places = -2) is 7, which is greater than 5. Hence, the digit before this i.e. 7 would be rounded off to 8.
4) Round a number using an expression:
Code
Output
The round function can solve expressions and then round the number after solving the expression
5) Round a number using a column:
Code
Output
This query selects the product_name column and rounds the values in the price column to 2 decimal places, resulting in a table that shows the product name and its rounded price.
Conclusion
- MySQL ROUND() function is a useful tool for rounding numbers to a specific number of decimal places.
- The ROUND() function takes two parameters:
- the number to be rounded
- the number of decimal places to which the number should be rounded. This parameter is optional
- The function returns a numeric value that is rounded to the specified number of decimal places.
- If decimal_places is positive, it rounds to the right of the decimal point, otherwise, it rounds to the left of the decimal point.
See Also
These functions are related to the ROUND() function
- FLOOR()
- CEIL()
- CEILING()
- TRUNCATE()