MySQL ROUND() Function with Examples

How to Use the MySQL ROUND() Function

In this tutorial, we'll learn how to use the MySQL ROUND() function. ROUND() is a function to round a number to a specified number of decimal paces.

MySQL ROUND() Syntax

The syntax of the ROUND() function is as follows:

ROUND(number, digits)

or

ROUND(number)

Remarks

  • The number argument is required.
  • The digits argument is optional and defaults to 0 if not specified. It can be positive or negative number.

Examples

Example 1: Round a Number to 2 Decimal Places

The following statement rounds the number to 2 decimal places:

SELECT ROUND(2000.4598, 2);

Result:

2000.46

Example 2: Round a Number to 0 Decimal Places

The following statement rounds the number to 0 decimal places:

SELECT ROUND(2000.4598);

or:

SELECT ROUND(2000.4598, 0);

Result:

2000

Example 3: Round a Number to -1 Decimal Places

The following statement rounds the number to -1 decimal places:

SELECT ROUND(25.298, -1);

Result:

30

Example 4: Round a Number to -1 Decimal Places

The following statement rounds the number to -1 decimal places:

SELECT ROUND(24.298, -1);

Result:

20

Example 5: Round a Number to -2 Decimal Places

The following statement rounds the number to -2 decimal places:

SELECT ROUND(25.298, -2);

Result:

0

Example 6: Round a Number to 3 Decimal Places

The following the number to 3 decimal places:

SELECT ROUND(20.29, 3);

Result:

The rounded value is 20.29, which is the same as the number since the number has 2 decimal places but we're trying to round it to 3 decimal places.

Example 7: ROUND(), SUM() and COUNT()

The following statement rounds the number, calculated by SUM() / COUNT() in the orders table, to 2 decimal places:

SELECT ROUND((SUM(o.amount) / COUNT(o.order_id)), 2)
FROM orders o;

Result:

For example, if the result returned from SUM() / COUNT() is 1988.775000, then rounded value is 1988.78.

In this tutorial, we've learned how to use the MySQL ROUND() function. The ROUND() function is used to round a number to a specified number of decimal places. The number of decimal places can be positive or negative.


See also:
MySQL LIKE Operator Pattern Matching and Examples
MySQL SUBSTRING_INDEX Function with Examples
MySQL EXISTS Operator with Examples
MySQL ROW_NUMBER Function with Examples
MySQL CONCAT() Function | Concatenate Strings in MySQL

Leave a Comment