Skip to content

Commit

Permalink
[doc] function truncate update (apache#516)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiqiang-hhhh authored Apr 8, 2024
1 parent ad251ae commit 49a1660
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 24 deletions.
27 changes: 21 additions & 6 deletions docs/sql-manual/sql-functions/numeric-functions/truncate.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@ under the License.

### description
#### Syntax
`DOUBLE truncate(DOUBLE x, INT d)`

`DOUBLE truncate(DOUBLE x, INT d)`
Numerically truncate `x` according to the number of decimal places `d`.
Numerically truncate `x` according to the number of decimal places `d`.

The rules are as follows:
When `d > 0`: keep `d` decimal places of `x`
When `d = 0`: remove the fractional part of `x` and keep only the integer part
When `d < 0`: Remove the fractional part of `x`, and replace the integer part with the number `0` according to the number of digits specified by `d`
The rules are as follows:

If `d` is literal:
When `d > 0`: keep `d` decimal places of `x`
When `d = 0`: remove the fractional part of `x` and keep only the integer part
When `d < 0`: Remove the fractional part of `x`, and replace the integer part with the number `0` according to the number of digits specified by `d`

Else if `d` is a column, and `x` has Decimal type, scale of result Decimal will always be same with input Decimal.

### example

Expand All @@ -56,6 +60,17 @@ mysql> select 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ under the License.
### description
#### Syntax

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

规则如下:
`d > 0`时:保留`x``d`位小数
`d = 0`时:将`x`的小数部分去除,只保留整数部分
`d < 0`时:将`x`的小数部分去除,整数部分按照 `d`所指定的位数,采用数字`0`进行替换
规则如下:

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

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

### example

Expand All @@ -56,6 +60,17 @@ mysql> select 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ under the License.
### description
#### Syntax

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

规则如下:
`d > 0`时:保留`x``d`位小数
`d = 0`时:将`x`的小数部分去除,只保留整数部分
`d < 0`时:将`x`的小数部分去除,整数部分按照 `d`所指定的位数,采用数字`0`进行替换
规则如下:

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

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

### example

Expand All @@ -56,6 +60,17 @@ mysql> select 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@ under the License.

### description
#### Syntax
`DOUBLE truncate(DOUBLE x, INT d)`

`DOUBLE truncate(DOUBLE x, INT d)`
Numerically truncate `x` according to the number of decimal places `d`.
Numerically truncate `x` according to the number of decimal places `d`.

The rules are as follows:
When `d > 0`: keep `d` decimal places of `x`
When `d = 0`: remove the fractional part of `x` and keep only the integer part
When `d < 0`: Remove the fractional part of `x`, and replace the integer part with the number `0` according to the number of digits specified by `d`
The rules are as follows:

If `d` is literal:
When `d > 0`: keep `d` decimal places of `x`
When `d = 0`: remove the fractional part of `x` and keep only the integer part
When `d < 0`: Remove the fractional part of `x`, and replace the integer part with the number `0` according to the number of digits specified by `d`

Else if `d` is a column, and `x` has Decimal type, scale of result Decimal will always be same with input Decimal.

### example

Expand All @@ -56,6 +60,17 @@ mysql> select 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
Expand Down

0 comments on commit 49a1660

Please sign in to comment.