环境:CentOS7.6 MySQL5.7.26
一、round函数使用说明
1、ROUND(x) 函数返回最接近于参数 x 的整数(四舍五入);
2、ROUND(x,y) 函数对参数x进行四舍五入的操作,返回值保留小数点后面指定的y位。
注:y为0时,即ROUND(X),返回四舍五入后的整数部分。
y为正数时,表示保留几位小数,小数位数不足则补0,过长则四舍五入后舍掉多余部分。
y为负数时,从个位往左数,第|y|位进行四舍五入运算,算完后,第|y|位及右边的都为0。
二、举例测试
ROUND(x):
1、x为整数测试
mysql> select round(456),round(-456) from dual;
±-----------±------------+
| round(456) | round(-456) |
±-----------±------------+
| 456 | -456 |
±-----------±------------+
2、x为非整数测试
mysql> select round(456.538),round(456.358),round(-456.538) from dual;
±---------------±---------------±----------------+
| round(456.538) | round(456.358) | round(-456.538) |
±---------------±---------------±----------------+
| 457 | 456 | -457 |
±---------------±---------------±----------------+
mysql> select round(-456.358),round(0.358),round(0.0358) from dual;
±----------------±-------------±--------------+
| round(-456.358) | round(0.358) | round(0.0358) |
±----------------±-------------±--------------+
| -456 | 0 | 0 |
±----------------±-------------±--------------+
mysql> select round(-0.358),round(-0.538) from dual;
±--------------±--------------+
| round(-0.358) | round(-0.538) |
±--------------±--------------+
| 0 | -1 |
±--------------±--------------+
ROUND(x,y):
1、y为正数
mysql> select round(456.358,2),round(456,1) from dual;
±-----------------±-------------+
| round(456.358,2) | round(456,1) |
±-----------------±-------------+
| 456.36 | 456 |
±-----------------±-------------+
mysql> select round(-456.358,2),round(-456,1) from dual;
±------------------±--------------+
| round(-456.358,2) | round(-456,1) |
±------------------±--------------+
| -456.36 | -456 |
±------------------±--------------+
mysql> select round(456.538,4) from dual;
±-----------------+
| round(456.538,4) |
±-----------------+
| 456.5380 |
±-----------------+
2、y为负数
mysql> select round(456.538,-2),round(456.538,-3) from dual;
±------------------±------------------+
| round(456.538,-2) | round(456.538,-3) |
±------------------±------------------+
| 500 | 0 |
±------------------±------------------+
mysql> select round(-456.538,-3),round(-456.538,-2) from dual;
±-------------------±-------------------+
| round(-456.538,-3) | round(-456.538,-2) |
±-------------------±-------------------+
| 0 | -500 |
±-------------------±-------------------+
mysql> select round(-556.538,-3),round(-556.538,-4) from dual;
±-------------------±-------------------+
| round(-556.538,-3) | round(-556.538,-4) |
±-------------------±-------------------+
| -1000 | 0 |
±-------------------±-------------------+
3、y为0,即ROUND(x)