Merge pull request #29003 from den-crane/patch-27

Doc. Mathematical Round for Decimals, Bankers for Floats
This commit is contained in:
Maksim Kita 2021-09-15 11:10:44 +03:00 committed by GitHub
commit 02b72b0fca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 4 deletions

View File

@ -29,7 +29,7 @@ Returns the round number with largest absolute value that has an absolute value
Rounds a value to a specified number of decimal places.
The function returns the nearest number of the specified order. In case when given number has equal distance to surrounding numbers, the function uses bankers rounding for float number types and rounds away from zero for the other number types.
The function returns the nearest number of the specified order. In case when given number has equal distance to surrounding numbers, the function uses bankers rounding for float number types and rounds away from zero for the other number types (Decimal).
``` sql
round(expression [, decimal_places])
@ -49,7 +49,7 @@ The rounded number of the same type as the input number.
### Examples {#examples}
**Example of use**
**Example of use with Float**
``` sql
SELECT number / 2 AS x, round(x) FROM system.numbers LIMIT 3
@ -63,6 +63,20 @@ SELECT number / 2 AS x, round(x) FROM system.numbers LIMIT 3
└─────┴──────────────────────────┘
```
**Example of use with Decimal**
``` sql
SELECT cast(number / 2 AS Decimal(10,4)) AS x, round(x) FROM system.numbers LIMIT 3
```
``` text
┌──────x─┬─round(CAST(divide(number, 2), 'Decimal(10, 4)'))─┐
│ 0.0000 │ 0.0000 │
│ 0.5000 │ 1.0000 │
│ 1.0000 │ 1.0000 │
└────────┴──────────────────────────────────────────────────┘
```
**Examples of rounding**
Rounding to the nearest number.

View File

@ -27,7 +27,7 @@ N может быть отрицательным.
Округляет значение до указанного десятичного разряда.
Функция возвращает ближайшее значение указанного порядка. В случае, когда заданное число равноудалено от чисел необходимого порядка, функция возвращает то из них, которое имеет ближайшую чётную цифру (банковское округление).
Функция возвращает ближайшее значение указанного порядка. В случае, когда заданное число равноудалено от чисел необходимого порядка, для типов с плавающей точкой (Float32/64) функция возвращает то из них, которое имеет ближайшую чётную цифру (банковское округление), для типов с фиксированной точкой (Decimal) функция использует округление в бо́льшую по модулю сторону (математическое округление).
``` sql
round(expression [, decimal_places])
@ -47,7 +47,7 @@ round(expression [, decimal_places])
### Примеры {#primery}
**Пример использования**
**Пример использования с Float**
``` sql
SELECT number / 2 AS x, round(x) FROM system.numbers LIMIT 3
@ -61,6 +61,21 @@ SELECT number / 2 AS x, round(x) FROM system.numbers LIMIT 3
└─────┴──────────────────────────┘
```
**Пример использования с Decimal**
``` sql
SELECT cast(number / 2 AS Decimal(10,4)) AS x, round(x) FROM system.numbers LIMIT 3
```
``` text
┌──────x─┬─round(CAST(divide(number, 2), 'Decimal(10, 4)'))─┐
│ 0.0000 │ 0.0000 │
│ 0.5000 │ 1.0000 │
│ 1.0000 │ 1.0000 │
└────────┴──────────────────────────────────────────────────┘
```
**Примеры округления**
Округление до ближайшего числа.