Skip to content

Commit

Permalink
Update round to add mode (#2560)
Browse files Browse the repository at this point in the history
* Update 1.math.md

* update

* update

* update
  • Loading branch information
ChrisChen2023 authored May 9, 2024
1 parent e153f27 commit f8ee751
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 4 deletions.
55 changes: 53 additions & 2 deletions docs-2.0-en/3.ngql-guide/6.functions-and-expressions/1.math.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,19 @@ nebula> RETURN ceil(9.1);

round() returns the rounded value of the specified number. Pay attention to the floating-point precision when using this function.

Syntax: `round(<expression>, <digit>)`
Syntax: `round(<expression>, <precision>, <mode>)`

- `expression`: An expression of which the result type is double.

- `digit`: Decimal digits. If `digit` is less than 0, round at the left of the decimal point.
- `precision`: An integer specifying the precision. If `precision` is less than 0, round at the left of the decimal point.

- `mode`: A string specifying the rounding mode. Optional. The following valid values are supported:
- `DOWN`: Round towards zero.
- `CEILING`: Round towards positive infinity.
- `FLOOR`: Round towards negative infinity.
- `HALF_UP`: Round towards the closest value of the given precision, with ties being rounded away from zero.
- `HALF_DOWN`: Round towards the closest value of the given precision, with ties being rounded towards zero.
- `HALF_EVEN`: Round towards the closest value of the given precision, with ties being rounded to the nearest even number.
- Result type: Double

Example:
Expand All @@ -92,12 +99,56 @@ nebula> RETURN round(314.15926, 2);
+--------------------+
| 314.16 |
+--------------------+
nebula> RETURN round(314.15926, -1);
+-----------------------+
| round(314.15926,-(1)) |
+-----------------------+
| 310.0 |
+-----------------------+
nebula> RETURN round(314.15926, 2, "DOWN");
+---------------------------+
| round(314.15926,2,"DOWN") |
+---------------------------+
| 314.15 |
+---------------------------+
nebula> RETURN round(314.15926, 2, "CEILING");
+------------------------------+
| round(314.15926,2,"CEILING") |
+------------------------------+
| 314.16 |
+------------------------------+
nebula> RETURN round(314.15926, 2, "FLOOR");
+----------------------------+
| round(314.15926,2,"FLOOR") |
+----------------------------+
| 314.15 |
+----------------------------+
nebula> RETURN round(314.15, 1, "HALF_UP");
+---------------------------+
| round(314.15,1,"HALF_UP") |
+---------------------------+
| 314.2 |
+---------------------------+
nebula> RETURN round(314.15, 1, "HALF_DOWN");
+-----------------------------+
| round(314.15,1,"HALF_DOWN") |
+-----------------------------+
| 314.1 |
+-----------------------------+
nebula> RETURN round(314.15, 1, "HALF_EVEN");
+-----------------------------+
| round(314.15,1,"HALF_EVEN") |
+-----------------------------+
| 314.2 |
+-----------------------------+
```

## sqrt()
Expand Down
53 changes: 51 additions & 2 deletions docs-2.0-zh/3.ngql-guide/6.functions-and-expressions/1.math.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,19 @@ nebula> RETURN ceil(9.1);

round() 返回指定数字四舍五入后的值。极端情况下请注意浮点数的精度问题。

语法:`round(<expression>, <digit>)`
语法:`round(<expression>, <precision>, <mode>)`

- `expression`:结果的数据类型为 double 的表达式。

- `digit`:小数位数。小于 0 时 ,在小数点左侧做四舍五入。数据类型为 int
- `precision`:代表精度的整数。小于 0 时 ,在小数点左侧做四舍五入。

- `mode`:代表舍入模式的字符串。可选参数。可选值如下:
- `DOWN`:向零舍入。
- `CEILING`:向正无穷舍入。
- `FLOOR`:向负无穷舍入。
- `HALF_UP`:基于精度向最接近的值舍入,如果最接近的值有两个,则取远离零的值。
- `HALF_DOWN`:基于精度向最接近的值舍入,如果最接近的值有两个,则取靠近零的值。
- `HALF_EVEN`:基于精度向最接近的值舍入,如果最接近的值有两个,则取最接近的偶数值。
- 返回类型:double。

示例:
Expand All @@ -100,6 +107,48 @@ nebula> RETURN round(314.15926, -1);
+-----------------------+
| 310.0 |
+-----------------------+
nebula> RETURN round(314.15926, 2, "DOWN");
+---------------------------+
| round(314.15926,2,"DOWN") |
+---------------------------+
| 314.15 |
+---------------------------+
nebula> RETURN round(314.15926, 2, "CEILING");
+------------------------------+
| round(314.15926,2,"CEILING") |
+------------------------------+
| 314.16 |
+------------------------------+
nebula> RETURN round(314.15926, 2, "FLOOR");
+----------------------------+
| round(314.15926,2,"FLOOR") |
+----------------------------+
| 314.15 |
+----------------------------+
nebula> RETURN round(314.15, 1, "HALF_UP");
+---------------------------+
| round(314.15,1,"HALF_UP") |
+---------------------------+
| 314.2 |
+---------------------------+
nebula> RETURN round(314.15, 1, "HALF_DOWN");
+-----------------------------+
| round(314.15,1,"HALF_DOWN") |
+-----------------------------+
| 314.1 |
+-----------------------------+
nebula> RETURN round(314.15, 1, "HALF_EVEN");
+-----------------------------+
| round(314.15,1,"HALF_EVEN") |
+-----------------------------+
| 314.2 |
+-----------------------------+
```

## sqrt()
Expand Down

0 comments on commit f8ee751

Please sign in to comment.