2020-04-03 13:23:32 +00:00
---
2022-04-03 23:05:35 +00:00
toc_priority: 45
toc_title: Rounding
2020-04-03 13:23:32 +00:00
---
2020-04-30 18:19:18 +00:00
# Rounding Functions {#rounding-functions}
2017-04-03 19:49:50 +00:00
2020-03-20 10:10:48 +00:00
## floor(x\[, N\]) {#floorx-n}
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
Returns the largest round number that is less than or equal to `x` . A round number is a multiple of 1/10N, or the nearest number of the appropriate data type if 1 / 10N isn’ t exact.
‘ N’ is an integer constant, optional parameter. By default it is zero, which means to round to an integer.
‘ N’ may be negative.
2017-04-03 19:49:50 +00:00
2017-12-28 15:13:23 +00:00
Examples: `floor(123.45, 1) = 123.4, floor(123.45, -1) = 120.`
2017-04-03 19:49:50 +00:00
2017-12-28 15:13:23 +00:00
`x` is any numeric type. The result is a number of the same type.
2021-05-27 19:44:11 +00:00
For integer arguments, it makes sense to round with a negative `N` value (for non-negative `N` , the function does not do anything).
2017-12-28 15:13:23 +00:00
If rounding causes overflow (for example, floor(-128, -1)), an implementation-specific result is returned.
2020-03-20 10:10:48 +00:00
## ceil(x\[, N\]), ceiling(x\[, N\]) {#ceilx-n-ceilingx-n}
2017-04-03 19:49:50 +00:00
2019-12-10 06:21:43 +00:00
Returns the smallest round number that is greater than or equal to `x` . In every other way, it is the same as the `floor` function (see above).
2017-04-03 19:49:50 +00:00
2020-03-20 10:10:48 +00:00
## trunc(x\[, N\]), truncate(x\[, N\]) {#truncx-n-truncatex-n}
2019-12-10 06:21:43 +00:00
2020-03-20 10:10:48 +00:00
Returns the round number with largest absolute value that has an absolute value less than or equal to `x` ‘ s. In every other way, it is the same as the ’ floor’ function (see above).
2019-12-10 06:21:43 +00:00
2020-03-22 09:14:59 +00:00
## round(x\[, N\]) {#rounding_functions-round}
2017-12-28 15:13:23 +00:00
2019-04-10 07:17:40 +00:00
Rounds a value to a specified number of decimal places.
2018-09-05 08:41:04 +00:00
2021-09-14 03:19:17 +00:00
The function returns the nearest number of the specified order. In case when given number has equal distance to surrounding numbers, the function uses banker’ s rounding for float number types and rounds away from zero for the other number types (Decimal).
2018-09-05 08:41:04 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-04-10 07:17:40 +00:00
round(expression [, decimal_places])
```
2021-03-13 18:18:45 +00:00
**Arguments**
2019-04-10 07:17:40 +00:00
2020-06-18 08:24:31 +00:00
- `expression` — A number to be rounded. Can be any [expression ](../../sql-reference/syntax.md#syntax-expressions ) returning the numeric [data type ](../../sql-reference/data-types/index.md#data_types ).
2020-03-21 04:11:51 +00:00
- `decimal-places` — An integer value.
- If `decimal-places > 0` then the function rounds the value to the right of the decimal point.
- If `decimal-places < 0` then the function rounds the value to the left of the decimal point.
- If `decimal-places = 0` then the function rounds the value to integer. In this case the argument can be omitted.
2018-09-05 08:41:04 +00:00
**Returned value:**
2019-04-10 07:17:40 +00:00
The rounded number of the same type as the input number.
2018-09-05 08:41:04 +00:00
2020-03-20 10:10:48 +00:00
### Examples {#examples}
2019-04-10 07:17:40 +00:00
2021-09-14 03:19:17 +00:00
**Example of use with Float**
2018-09-05 08:41:04 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2019-04-10 07:17:40 +00:00
SELECT number / 2 AS x, round(x) FROM system.numbers LIMIT 3
2018-09-05 08:41:04 +00:00
```
2020-03-20 10:10:48 +00:00
``` text
2018-09-05 08:41:04 +00:00
┌───x─┬─round(divide(number, 2))─┐
│ 0 │ 0 │
│ 0.5 │ 0 │
│ 1 │ 1 │
└─────┴──────────────────────────┘
```
2017-04-03 19:49:50 +00:00
2021-09-14 03:19:17 +00:00
**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 │
└────────┴──────────────────────────────────────────────────┘
```
2019-04-10 07:17:40 +00:00
**Examples of rounding**
Rounding to the nearest number.
2020-03-20 10:10:48 +00:00
``` text
2019-04-10 07:17:40 +00:00
round(3.2, 0) = 3
round(4.1267, 2) = 4.13
round(22,-1) = 20
round(467,-2) = 500
round(-467,-2) = -500
```
2020-03-20 10:10:48 +00:00
Banker’ s rounding.
2019-04-10 07:17:40 +00:00
2020-03-20 10:10:48 +00:00
``` text
2019-04-10 07:17:40 +00:00
round(3.5) = 4
round(4.5) = 4
round(3.55, 1) = 3.6
round(3.65, 1) = 3.6
```
2020-03-20 10:10:48 +00:00
**See Also**
2020-01-17 06:12:43 +00:00
2020-03-21 04:11:51 +00:00
- [roundBankers ](#roundbankers )
2020-01-17 06:12:43 +00:00
2020-03-18 18:43:51 +00:00
## roundBankers {#roundbankers}
2020-01-17 06:12:43 +00:00
2020-01-17 06:56:17 +00:00
Rounds a number to a specified decimal position.
2020-01-17 06:12:43 +00:00
2020-03-21 04:11:51 +00:00
- If the rounding number is halfway between two numbers, the function uses banker’ s rounding.
2020-01-17 06:12:43 +00:00
2020-03-21 04:11:51 +00:00
Banker's rounding is a method of rounding fractional numbers. When the rounding number is halfway between two numbers, it's rounded to the nearest even digit at the specified decimal position. For example: 3.5 rounds up to 4, 2.5 rounds down to 2.
2020-03-20 10:10:48 +00:00
2020-03-21 04:11:51 +00:00
It's the default rounding method for floating point numbers defined in [IEEE 754 ](https://en.wikipedia.org/wiki/IEEE_754#Roundings_to_nearest ). The [round ](#rounding_functions-round ) function performs the same rounding for floating point numbers. The `roundBankers` function also rounds integers the same way, for example, `roundBankers(45, -1) = 40` .
2020-01-17 06:42:18 +00:00
2020-03-21 04:11:51 +00:00
- In other cases, the function rounds numbers to the nearest integer.
2020-01-17 06:42:18 +00:00
2020-03-20 10:10:48 +00:00
Using banker’ s rounding, you can reduce the effect that rounding numbers has on the results of summing or subtracting these numbers.
2020-01-17 06:12:43 +00:00
For example, sum numbers 1.5, 2.5, 3.5, 4.5 with different rounding:
2020-03-21 04:11:51 +00:00
- 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.
2020-01-17 06:12:43 +00:00
2020-03-20 10:10:48 +00:00
**Syntax**
2020-01-17 06:12:43 +00:00
2020-03-20 10:10:48 +00:00
``` sql
2020-01-17 06:12:43 +00:00
roundBankers(expression [, decimal_places])
```
2021-02-15 21:22:10 +00:00
**Arguments**
2020-01-17 06:12:43 +00:00
2020-06-18 08:24:31 +00:00
- `expression` — A number to be rounded. Can be any [expression ](../../sql-reference/syntax.md#syntax-expressions ) returning the numeric [data type ](../../sql-reference/data-types/index.md#data_types ).
2020-03-21 04:11:51 +00:00
- `decimal-places` — Decimal places. An integer number.
- `decimal-places > 0` — The function rounds the number to the given position right of the decimal point. Example: `roundBankers(3.55, 1) = 3.6` .
- `decimal-places < 0` — The function rounds the number to the given position left of the decimal point. Example: `roundBankers(24.55, -1) = 20` .
- `decimal-places = 0` — The function rounds the number to an integer. In this case the argument can be omitted. Example: `roundBankers(2.5) = 2` .
2020-01-17 06:12:43 +00:00
**Returned value**
2020-03-20 10:10:48 +00:00
A value rounded by the banker’ s rounding method.
2020-01-17 06:12:43 +00:00
2020-03-20 10:10:48 +00:00
### Examples {#examples-1}
2020-01-17 06:12:43 +00:00
**Example of use**
Query:
2020-03-20 10:10:48 +00:00
``` sql
2020-01-17 06:12:43 +00:00
SELECT number / 2 AS x, roundBankers(x, 0) AS b fROM system.numbers limit 10
```
Result:
2020-03-20 10:10:48 +00:00
``` text
2020-01-17 06:12:43 +00:00
┌───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 │
└─────┴───┘
```
2020-03-20 10:10:48 +00:00
**Examples of Banker’ s rounding**
2020-01-17 06:12:43 +00:00
2020-03-20 10:10:48 +00:00
``` text
2020-01-17 06:12:43 +00:00
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
2021-09-22 08:12:35 +00:00
roundBankers(10.755, 2) = 10.76
2020-01-17 06:12:43 +00:00
```
2020-03-20 10:10:48 +00:00
**See Also**
2020-01-17 06:12:43 +00:00
2020-03-21 04:11:51 +00:00
- [round ](#rounding_functions-round )
2020-01-17 06:12:43 +00:00
2020-03-20 10:10:48 +00:00
## roundToExp2(num) {#roundtoexp2num}
2017-12-28 15:13:23 +00:00
2017-04-26 19:16:38 +00:00
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.
2017-04-03 19:49:50 +00:00
2020-03-20 10:10:48 +00:00
## roundDuration(num) {#rounddurationnum}
2017-12-28 15:13:23 +00:00
2022-04-03 23:05:35 +00:00
Accepts a number. If the number is less than one, it returns 0. Otherwise, it rounds the number down to numbers from the set: 1, 10, 30, 60, 120, 180, 240, 300, 600, 1200, 1800, 3600, 7200, 18000, 36000. This function was specifically implemented for a web analytics use case for reporting on session lengths.
2017-12-28 15:13:23 +00:00
2020-03-20 10:10:48 +00:00
## roundAge(num) {#roundagenum}
2017-12-28 15:13:23 +00:00
2022-03-12 06:24:31 +00:00
Accepts a number. If the number is less than 18, it returns 0. Otherwise, it rounds the number down to a number from the set: 18, 25, 35, 45, 55.
2017-04-03 19:49:50 +00:00
2020-03-20 10:10:48 +00:00
## roundDown(num, arr) {#rounddownnum-arr}
2019-01-30 10:39:46 +00:00
2020-01-27 14:02:08 +00:00
Accepts a number and rounds it down to an element in the specified array. If the value is less than the lowest bound, the lowest bound is returned.
2018-10-16 10:47:17 +00:00