MySQL tutorial: mysql round() function

MySQL ROUND() is a mathematical function that rounds Numbers to a specified number of decimal places.

Syntax

ROUND(X,[D])

Rounds the argument X to D decimal places. The maximum absolute value for D is 30; any digits in excess of 30 (or -30) are truncated.

Argument

XRequired, A number which will be rounded upto D decimal places.
DOptional, represents a maximum rounding of the decimal number X.

MySQL Version: 5.7

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.28    |
+-----------+
1 row in set (0.01 sec)

Examples

In the following MySQL example, no parameter “D” is specified, so the default value of “D” is 0.
If “D” is 0, rounding starts to the left of the decimal “D” of the value “X”.

mysql> select round(5.22) \G
round(5.22): 5

mysql> select round(5.52) \G
round(5.52): 6

mysql> select round(-5.52) \G
round(-5.52): -6

mysql> select round(-5.12) \G
round(-5.12): -5

In the following example, round the given number -5.617 up to 2 decimal places.

mysql> select round(-5.617, 2) \G
round(-5.617, 2): -5.62

In the following MySQL example, the given number 84.5119 from the left of decimal place up to 1 place.

mysql> select round(84.5119, -1) \G
round(84.5119, -1): 80

mysql> select round(85.5119, -1) \G
round(85.5119, -1): 90

In the following MySQL example, if the parameter D is greater than 30, anything above 30 will be truncated.

mysql> select round(0.1234567890123456789012345678901234567890, 35) \G
round(0.1234567890123456789012345678901234567890, 35): 0.123456789012345678901234567890

Add a Comment

Your email address will not be published. Required fields are marked *