跳到主要内容
跳到主要内容

TRUNCATE

truncate

description

Syntax

DOUBLE truncate(DOUBLE x, INT d)
按照保留小数的位数dx进行数值截取。

规则如下:

如果 d 为字面量:
d > 0时:保留xd位小数
d = 0时:将x的小数部分去除,只保留整数部分
d < 0时:将x的小数部分去除,整数部分按照 d所指定的位数,采用数字0进行替换

如果 d 为一个列,并且第一个参数为 Decimal 类型,那么结果 Decimal 会跟入参 Decimal 具有相同的小数部分长度。

example

mysql> select truncate(124.3867, 2);
+-----------------------+
| truncate(124.3867, 2) |
+-----------------------+
| 124.38 |
+-----------------------+
mysql> select truncate(124.3867, 0);
+-----------------------+
| truncate(124.3867, 0) |
+-----------------------+
| 124 |
+-----------------------+
mysql> select truncate(-124.3867, -2);
+-------------------------+
| truncate(-124.3867, -2) |
+-------------------------+
| -100 |
+-------------------------+
mysql> select cast("123.123456" as Decimal(9,6)), number, truncate(cast ("123.123456" as Decimal(9,6)), number) from numbers("number"="5");
--------------
+---------------------------------------+--------+----------------------------------------------------------------------+
| cast('123.123456' as DECIMALV3(9, 6)) | number | truncate(cast('123.123456' as DECIMALV3(9, 6)), cast(number as INT)) |
+---------------------------------------+--------+----------------------------------------------------------------------+
| 123.123456 | 0 | 123.000000 |
| 123.123456 | 1 | 123.100000 |
| 123.123456 | 2 | 123.120000 |
| 123.123456 | 3 | 123.123000 |
| 123.123456 | 4 | 123.123400 |
+---------------------------------------+--------+----------------------------------------------------------------------+

keywords

TRUNCATE