ROUND()
Function Description
The ROUND() function returns the value of a number after rounding a specific digit.
This function returns the closest number in the specified number of digits. If the given number is equal to the surrounding number (for example, 5), then the "banker's rounding" method will be rounded.
Function Syntax
> ROUND(number, decimals)
> ROUND(number)
Parameter definition
| Parameters | Description |
|---|---|
| number | Required parameters, if you want to round, you can take any numerical data type |
| decimals | Optional parameter, indicating the number of digits after the decimal point to be rounded. The default value is 0, which means rounding to an integer. decimals>0 The function will round to the number of digits after the decimal point decimals<0 The function will round to the number of digits before the decimal point decimals=0 The function will round to the integer |
Example
drop table if exists t1;
create table t1(a int ,b float);
insert into t1 values(1,0.5);
insert into t1 values(2,0.499);
insert into t1 values(3,0.501);
insert into t1 values(4,20.5);
insert into t1 values(5,20.499);
insert into t1 values(6,13.500);
insert into t1 values(7,-0.500);
insert into t1 values(8,-0.499);
insert into t1 values(9,-0.501);
insert into t1 values(10,-20.499);
insert into t1 values(11,-20.500);
insert into t1 values(12,-13.500);
mysql> select a,round(b) from t1;
+------+--------------+
| a | round(b) |
+------+--------------+
| 1 | 0 |
| 2 | 0 |
| 3 | 1 |
| 4 | 20 |
| 5 | 20 |
| 6 | 14 |
| 7 | -0 |
| 8 | -0 |
| 9 | -1 |
| 10 | -20 |
| 11 | -20 |
| 12 | -14 |
+------+--------------+
12 rows in set (0.00 sec)
mysql> select a,round(b,-1) from t1;
+---------------------------+
| a | round(b, -1) |
+---------------------------+
| 1 | 0 |
| 2 | 0 |
| 3 | 0 |
| 4 | 20 |
| 5 | 20 |
| 6 | 10 |
| 7 | -0 |
| 8 | -0 |
| 9 | -0 |
| 10 | -20 |
| 11 | -20 |
| 12 | -10 |
+---------------------------+
12 rows in set (0.01 sec)
mysql> select round(a*b) from t1;
+-------------------+
| round(a * b) |
+-------------------+
| 0 |
| 1 |
| 2 |
| 82 |
| 102 |
| 81 |
| -4 |
| -4 |
| -5 |
| -205 |
| -226 |
| -162 |
+-------------------+
12 rows in set (0.01 sec)