目录
1、CEIL()、CEILING()
2、FLOOR()
3、ROUND()、ROUND(X,D)
4、TRUNCATE()
CEIL() | 返回不小于参数的最小整数值 |
CEILING() | 返回不小于参数的最小整数值 |
FLOOR() | 返回不大于参数的最大整数值 |
ROUND() | 返回参数的四舍五入值 |
TRUNCATE() | 返回参数舍入到的小数位 |
解释:返回不小于参数的最小整数值
CEIL() 是 CEILING() 的同义词。
mysql> SELECT CEIL(9.12), CEIL(-9.12);
+------------+-------------+
| CEIL(9.12) | CEIL(-9.12) |
+------------+-------------+
| 10 | -9 |
+------------+-------------+
1 row in set (0.01 sec)
mysql> SELECT CEILING(9.12), CEILING(-9.12);
+---------------+----------------+
| CEILING(9.12) | CEILING(-9.12) |
+---------------+----------------+
| 10 | -9 |
+---------------+----------------+
1 row in set (0.01 sec)
解释:返回不大于参数的最大整数值
mysql> SELECT FLOOR(9.12), FLOOR(-9.12);
+-------------+--------------+
| FLOOR(9.12) | FLOOR(-9.12) |
+-------------+--------------+
| 9 | -10 |
+-------------+--------------+
1 row in set (0.01 sec)
解释:
将参数X四舍五入到小数点后第 D 位。舍入算法取决于数据类型。
如果 D 未指定,则默认为 0,
D 可以是负的,则X值小数点左边的D位数都为零,
D 的最大绝对值为30,任何超过30(或-30)的数字都会被截断
mysql> SELECT ROUND(-9.12),ROUND(-9.58),ROUND(9.58);
+--------------+--------------+-------------+
| ROUND(-9.12) | ROUND(-9.58) | ROUND(9.58) |
+--------------+--------------+-------------+
| -9 | -10 | 10 |
+--------------+--------------+-------------+
1 row in set (0.02 sec)
mysql> SELECT ROUND(9.298, 1),ROUND(9.298, 0),ROUND(23.298, -1);
+-----------------+-----------------+-------------------+
| ROUND(9.298, 1) | ROUND(9.298, 0) | ROUND(23.298, -1) |
+-----------------+-----------------+-------------------+
| 9.3 | 9 | 20 |
+-----------------+-----------------+-------------------+
1 row in set (0.03 sec)
mysql> SELECT ROUND(0.12345678901234567890123456789012345, 35);
+--------------------------------------------------+
| ROUND(0.12345678901234567890123456789012345, 35) |
+--------------------------------------------------+
| 0.123456789012345678901234567890 |
+--------------------------------------------------+
1 row in set (0.04 sec)
解释:
返回舍入到的小数位的参数值。
如果 D 未指定,则默认为 0,
D 可以是负的,则X值小数点左边的D位数都为零,
D 的最大绝对值为30,任何超过30(或-30)的数字都会被截断
注:所有数字均四舍五入为零
mysql> SELECT TRUNCATE(-9.12,0),TRUNCATE(-9.58,0),TRUNCATE(9.58,0);
+-------------------+-------------------+------------------+
| TRUNCATE(-9.12,0) | TRUNCATE(-9.58,0) | TRUNCATE(9.58,0) |
+-------------------+-------------------+------------------+
| -9 | -9 | 9 |
+-------------------+-------------------+------------------+
1 row in set (0.12 sec)
mysql>
SELECT TRUNCATE(9.298, 1),TRUNCATE(23.298, -1),TRUNCATE(890912.298, -5);
+--------------------+----------------------+--------------------------+
| TRUNCATE(9.298, 1) | TRUNCATE(23.298, -1) | TRUNCATE(890912.298, -5) |
+--------------------+----------------------+--------------------------+
| 9.2 | 20 | 800000 |
+--------------------+----------------------+--------------------------+
1 row in set (0.12 sec)
mysql> SELECT TRUNCATE(0.12345678901234567890123456789012345, 35);
+-----------------------------------------------------+
| TRUNCATE(0.12345678901234567890123456789012345, 35) |
+-----------------------------------------------------+
| 0.123456789012345678901234567890 |
+-----------------------------------------------------+
1 row in set (0.13 sec)
补充:在MySQL 8.0.21及以后版本中,TRUNCATE()返回的数据类型与ROUND()函数的返回类型相同;
本文的代码集:
-- CEIL()、CEILING()
SELECT CEIL(9.12), CEIL(-9.12);
SELECT CEILING(9.12), CEILING(-9.12);
-- FLOOR()
SELECT FLOOR(9.12), FLOOR(-9.12);
-- ROUND(X), ROUND(X,D)
SELECT ROUND(-9.12),ROUND(-9.58),ROUND(9.58);
SELECT ROUND(9.298, 1),ROUND(9.298, 0),ROUND(23.298, -1); -- D 为负的时候小数点左边的D位为 0
SELECT ROUND(0.12345678901234567890123456789012345, 35); -- D 超过30时,会截断
-- TRUNCATE()
SELECT TRUNCATE(-9.12,0),TRUNCATE(-9.58,0),TRUNCATE(9.58,0);
SELECT TRUNCATE(9.298, 1),TRUNCATE(23.298, -1),TRUNCATE(890912.298, -5); -- D 为负的时候小数点左边的D位为 0
SELECT TRUNCATE(0.12345678901234567890123456789012345, 35); -- D 超过30时,会截断
若本文有不妥的地方,望指证,谢谢
参考:https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html