四舍五入函数ROUND(x)、ROUND(x,y)和TRUNCATE(x,y)
代码如下:
mysql> SELECT ROUND(2.3), ROUND(2.5), ROUND(2.53,1), ROUND(2.55,1);
+------------+------------+---------------+---------------+
| ROUND(2.3) | ROUND(2.5) | ROUND(2.53,1) | ROUND(2.55,1) |
+------------+------------+---------------+---------------+
| 2 | 3 | 2.5 | 2.6 |
+------------+------------+---------------+---------------+
row in set (0.00 sec)
ROUND(x)返回离x最近的整数,也就是对x进行四舍五入处理
ROUND(x,y)返回x保留到小数点后y位的值,在截取时进行四舍五入处理
再看下面实例:
mysql> SELECT TRUNCATE(2.53,1), TRUNCATE(2.55,1);
+------------------+------------------+
| TRUNCATE(2.53,1) | TRUNCATE(2.55,1) |
+------------------+------------------+
| 2.5 | 2.5 |
+------------------+------------------+
row in set (0.00 sec)
TRUNCATE(x,y)返回数值x保留到小数点后y位的值(与ROUND最大的区别是不会进行四舍五入)
实例如下:
SELECT TRUNCATE(1.23456,3)
结果:1.234