elenbaskakova-DOCSUP-784 (#79)

* docs(roundbankers):The description of `roundBankers` function was added.

* docs(roundbankers):The description of `roundBankers` function was edited.

* docs(roundbankers):The description of `roundBankers` function was edited.

* docs(roundbankers):The description of `roundBankers` function was edited.

* docs(roundbankers):The description of `roundBankers` function was edited.
This commit is contained in:
elenaspb2019 2020-01-17 09:12:43 +03:00 committed by BayoNet
parent decdda3110
commit 3f2d782dc2
2 changed files with 162 additions and 0 deletions

View File

@ -78,6 +78,87 @@ round(3.55, 1) = 3.6
round(3.65, 1) = 3.6
```
**See Also**
- [roundBankers](#roundbankers)
## roundBankers {#roundbankers}
Rounds a value using banker's rounding.
Banker's rounding is a method of rounding fractional numbers. When the rounding number is a half between two numbers, it is rounded to the nearest even number. E.g. 3.5 rounds up to 4, 2.5 rounds down to 2.
Using this method, you can reduce the effect of rounding numbers on the result of summing or subtracting these numbers.
For example, sum numbers 1.5, 2.5, 3.5, 4.5 with different rounding:
- No rounding: 1.5 + 2.5 + 3.5 + 4.5 = 12
- Banker's rounding: 2 + 2 + 4 + 4 = 12
- Rounding to the nearest integer: 2 + 3 + 4 + 5 = 14.
**Syntax**
```sql
roundBankers(expression [, decimal_places])
```
**Parameters**
- `expression` — A number to be rounded. Can be any [expression](../syntax.md#syntax-expressions) returning the numeric [data type](../../data_types/index.md#data_types).
- `decimal-places` — An integer value.
- `decimal-places > 0` — The function rounds the value to the given position right of the decimal point. E.g. roundBankers(3.55, 1).
- `decimal-places < 0` — The function rounds the value to the given position left of the decimal point. E.g. roundBankers(33.55, -1) = 30.
- `decimal-places = 0` — The function rounds the value to integer. In this case the argument can be omitted. E.g. roundBankers(2.5, 0) = 2.
**Returned value**
A value rounded by banker's rounding method.
### Examples
**Example of use**
Query:
```sql
SELECT number / 2 AS x, roundBankers(x, 0) AS b fROM system.numbers limit 10
```
Result:
```text
┌───x─┬─b─┐
│ 0 │ 0 │
│ 0.5 │ 0 │
│ 1 │ 1 │
│ 1.5 │ 2 │
│ 2 │ 2 │
│ 2.5 │ 2 │
│ 3 │ 3 │
│ 3.5 │ 4 │
│ 4 │ 4 │
│ 4.5 │ 4 │
└─────┴───┘
```
**Examples of Banker's rounding**
```text
roundBankers(0.4) = 0
roundBankers(-3.5) = -4
roundBankers(4.5) = 4
roundBankers(3.55, 1) = 3.6
roundBankers(3.65, 1) = 3.6
roundBankers(10.35, 1) = 10.4
roundBankers(10.755, 2) = 11,76
```
**See Also**
- [round](#rounding_functions-round)
## roundToExp2(num)
Accepts a number. If the number is less than one, it returns 0. Otherwise, it rounds the number down to the nearest (whole non-negative) degree of two.

View File

@ -75,6 +75,87 @@ round(3.55, 1) = 3.6
round(3.65, 1) = 3.6
```
**См. также**
- [roundBankers](#roundbankers)
## roundBankers {#roundbankers}
Функция предназначена для округления значений с использованием метода банковского округления.
Банковское округление (англ. banker's rounding) — метод округления дробных чисел. Если округляемое число является строго половиной между двумя числами, то оно округляется до ближайшего чётного числа. К примеру, 3,5 округляется до 4, а 2,5 до 2.
Метод позволяет уменьшить влияние округления чисел на результат суммирования или вычитания этих чисел.
Например, сумма чисел 1.5, 2.5, 3.5 и 4.5 с различным округлением:
- Без округления: 1.5 + 2.5 + 3.5 + 4.5 = 12
- Банковское округление: 2 + 2 + 4 + 4 = 12
- Округление до ближайшего целого: 2 + 3 + 4 + 5 = 14.
**Синтаксис**
```sql
roundBankers(expression [, decimal_places])
```
**Параметры**
- `expression` — Число для округления. Может быть любым [выражением](../syntax.md#syntax-expressions), возвращающим числовой [тип данных](../../data_types/index.md#data_types).
- `decimal-places` — Целое значение.
- `decimal-places > 0` — Функция округляет значение выражения до ближайшего чётного числа на соответствующей позиции справа от запятой. Например, roundBankers(3.55, 1).
- `decimal-places < 0` — Функция округляет значение выражения до ближайшего чётного числа на соответствующей позиции слева от запятой. Например, roundBankers(33.55, -1) = 30.
- `decimal-places = 0` — Функция округляет значение до целого. Например, roundBankers(2.5, 0) = 2.
**Возвращаемое значение**
Округлённое значение по методу банковского округления.
**Пример использования**
Запрос:
```sql
SELECT number / 2 AS x, roundBankers(x, 0) AS b fROM system.numbers limit 10
```
Результат:
```text
┌───x─┬─b─┐
│ 0 │ 0 │
│ 0.5 │ 0 │
│ 1 │ 1 │
│ 1.5 │ 2 │
│ 2 │ 2 │
│ 2.5 │ 2 │
│ 3 │ 3 │
│ 3.5 │ 4 │
│ 4 │ 4 │
│ 4.5 │ 4 │
└─────┴───┘
```
**Примеры банковского округления**
```text
roundBankers(0.4) = 0
roundBankers(-3.5) = -4
roundBankers(4.5) = 4
roundBankers(3.55, 1) = 3.6
roundBankers(3.65, 1) = 3.6
roundBankers(10.35, 1) = 10.4
roundBankers(10.755, 2) = 11,76
```
**См. также**
- [round](#rounding_functions-round)
## roundToExp2(num)
Принимает число. Если число меньше единицы - возвращает 0. Иначе округляет число вниз до ближайшей (целой неотрицательной) степени двух.