环境:CentOS7.6 MySQL5.7.26
一、TRUNCATE函数使用说明
TRUNCATE(X,D) 是MySQL自带的一个系统函数。
X是数值,D是截断数据的位置。
若D为0,则截去小数部分;
若D>0,则截去小数D位后面的数,如果位数不够,则补0;
若D<0,则截去整数D位及后面的数并变为0(小数位丢掉),如果位数不够,则返回0。(从小数点往左数)
二、举例测试
1、不设D值
select truncate(333) from dual;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘) from dual’ at line 1
2、D值为0
mysql> select truncate(456.63,0),truncate(456.63,0) from dual;
±-------------------±-------------------+
| truncate(456.63,0) | truncate(456.63,0) |
±-------------------±-------------------+
| 456 | 456 |
±-------------------±-------------------+
mysql> select truncate(456,0),truncate(654,0) from dual;
±----------------±----------------+
| truncate(456,0) | truncate(654,0) |
±----------------±----------------+
| 456 | 654 |
±----------------±----------------+
3、D值大于0
mysql> select truncate(456.63,1),truncate(456.63,3) from dual;
±-------------------±-------------------+
| truncate(456.63,1) | truncate(456.63,3) |
±-------------------±-------------------+
| 456.6 | 456.630 |
±-------------------±-------------------+
4、D值小于0
mysql> select truncate(456.63,-1),truncate(456.63,-4) from dual;
±--------------------±--------------------+
| truncate(456.63,-1) | truncate(456.63,-4) |
±--------------------±--------------------+
| 450 | 0 |
±--------------------±--------------------+
总结:TRUNCATE函数与ROUND函数区别在于是否进行四舍五入。




