Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update round to add mode #2560

Merged
merged 4 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
ChrisChen2023 marked this conversation as resolved.
Show resolved Hide resolved
- `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