> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-8a08bda2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Documentation for rounding functions

# Rounding functions

<Note>
  The documentation below is generated from the `system.functions` system table
</Note>

{/*AUTOGENERATED_START*/}

<h2 id="ceil">
  ceil
</h2>

Introduced in: v1.1.0

Like [`floor`](#floor) but returns the smallest rounded number greater than or equal to `x`.
If rounding causes an overflow (for example, `ceiling(255, -1)`), the result is undefined.

**Syntax**

```sql theme={null}
ceiling(x[, N])
```

**Aliases**: `ceiling`

**Arguments**

* `x` — The value to round. [`Float*`](/reference/data-types/float) or [`Decimal*`](/reference/data-types/decimal) or [`(U)Int*`](/reference/data-types/int-uint)
* `N` — Optional. The number of decimal places to round to. Defaults to zero, which means rounding to an integer. Can be negative. [`(U)Int*`](/reference/data-types/int-uint)

**Returned value**

Returns a rounded number of the same type as `x`. [`Float*`](/reference/data-types/float) or [`Decimal*`](/reference/data-types/decimal) or [`(U)Int*`](/reference/data-types/int-uint)

**Examples**

**Basic usage**

```sql title=Query theme={null}
SELECT ceiling(123.45, 1) AS rounded
```

```response title=Response theme={null}
┌─rounded─┐
│   123.5 │
└─────────┘
```

**Negative precision**

```sql title=Query theme={null}
SELECT ceiling(123.45, -1)
```

```response title=Response theme={null}
┌─ceiling(123.45, -1)─┐
│                 130 │
└─────────────────────┘
```

<h2 id="floor">
  floor
</h2>

Introduced in: v1.1.0

Returns the largest rounded number less than or equal to `x`, where the rounded number is a multiple of `1 / 10 * N`, or the nearest number of the appropriate data type if `1 / 10 * N` isn't exact.

Integer arguments may be rounded with a negative `N` argument.
With non-negative `N` the function returns `x`.

If rounding causes an overflow (for example, `floor(-128, -1)`), the result is undefined.

**Syntax**

```sql theme={null}
floor(x[, N])
```

**Arguments**

* `x` — The value to round. [`Float*`](/reference/data-types/float) or [`Decimal*`](/reference/data-types/decimal) or [`(U)Int*`](/reference/data-types/int-uint)
* `N` — Optional. The number of decimal places to round to. Defaults to zero, which means rounding to an integer. Can be negative. [`(U)Int*`](/reference/data-types/int-uint)

**Returned value**

Returns a rounded number of the same type as `x`. [`Float*`](/reference/data-types/float) or [`Decimal*`](/reference/data-types/decimal) or [`(U)Int*`](/reference/data-types/int-uint)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT floor(123.45, 1) AS rounded
```

```response title=Response theme={null}
┌─rounded─┐
│   123.4 │
└─────────┘
```

**Negative precision**

```sql title=Query theme={null}
SELECT floor(123.45, -1)
```

```response title=Response theme={null}
┌─floor(123.45, -1)─┐
│               120 │
└───────────────────┘
```

<h2 id="round">
  round
</h2>

Introduced in: v1.1.0

Rounds a value to a specified number of decimal places `N`.

* If `N > 0`, the function rounds to the right of the decimal point.
* If `N < 0`, the function rounds to the left of the decimal point.
* If `N = 0`, the function rounds to the next integer.

The function returns the nearest number of the specified order.
If the input value has equal distance to two neighboring numbers, the function uses banker's rounding for `Float*` inputs and rounds away from zero for the other number types (`Decimal*`).

If rounding causes an overflow (for example, `round(255, -1)`), the result is undefined.

**Syntax**

```sql theme={null}
round(x[, N])
```

**Arguments**

* `x` — A number to round. [`Float*`](/reference/data-types/float) or [`Decimal*`](/reference/data-types/decimal) or [`(U)Int*`](/reference/data-types/int-uint)
* `N` — Optional. The number of decimal places to round to. Defaults to `0`. [`(U)Int*`](/reference/data-types/int-uint)

**Returned value**

Returns a rounded number of the same type as `x`. [`Float*`](/reference/data-types/float) or [`Decimal*`](/reference/data-types/decimal) or [`(U)Int*`](/reference/data-types/int-uint)

**Examples**

**Float inputs**

```sql title=Query theme={null}
SELECT number / 2 AS x, round(x) FROM system.numbers LIMIT 3;
```

```response title=Response theme={null}
┌───x─┬─round(x)─┐
│   0 │        0 │
│ 0.5 │        0 │
│   1 │        1 │
└─────┴──────────┘
```

**Decimal inputs**

```sql title=Query theme={null}
SELECT cast(number / 2 AS  Decimal(10,4)) AS x, round(x) FROM system.numbers LIMIT 3;
```

```response title=Response theme={null}
┌───x─┬─round(x)─┐
│   0 │        0 │
│ 0.5 │        1 │
│   1 │        1 │
└─────┴──────────┘
```

<h2 id="roundAge">
  roundAge
</h2>

Introduced in: v1.1.0

Takes a number representing a human age, compares it to standard age ranges, and returns either the highest or lowest value of the range the number falls within.

* Returns `0`, for `age < 1`.
* Returns `17`, for `1 ≤ age ≤ 17`.
* Returns `18`, for `18 ≤ age ≤ 24`.
* Returns `25`, for `25 ≤ age ≤ 34`.
* Returns `35`, for `35 ≤ age ≤ 44`.
* Returns `45`, for `45 ≤ age ≤ 54`.
* Returns `55`, for `age ≥ 55`.

**Syntax**

```sql theme={null}
roundAge(num)
```

**Arguments**

* `age` — A number representing an age in years. [`(U)Int*`](/reference/data-types/int-uint) or [`Float*`](/reference/data-types/float)

**Returned value**

Returns either the highest or lowest age of the range `age` falls within. [`UInt8`](/reference/data-types/int-uint)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT *, roundAge(*) FROM system.numbers WHERE number IN (0, 5, 20, 31, 37, 54, 72);
```

```response title=Response theme={null}
┌─number─┬─roundAge(number)─┐
│      0 │                0 │
│      5 │               17 │
│     20 │               18 │
│     31 │               25 │
│     37 │               35 │
│     54 │               45 │
│     72 │               55 │
└────────┴──────────────────┘
```

<h2 id="roundBankers">
  roundBankers
</h2>

Introduced in: v20.1.0

Rounds a number to a specified decimal position `N`.
If the rounding number is halfway between two numbers, the function uses a method of rounding called banker's rounding, which is the default rounding method for floating point numbers defined in IEEE 754.

* If `N > 0`, the function rounds to the right of the decimal point
* If `N < 0`, the function rounds to the left of the decimal point
* If `N = 0`, the function rounds to the next integer

<Info>
  **Notes**

  * 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`.
  * The `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`.
  * In other cases, the function rounds numbers to the nearest integer.
</Info>

<Tip>
  **Use banker's rounding for summation or subtraction of numbers**

  Using banker's rounding, you can reduce the effect that rounding numbers has on the results 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`.
</Tip>

**Syntax**

```sql theme={null}
roundBankers(x[, N])
```

**Arguments**

* `x` — A number to round. [`(U)Int*`](/reference/data-types/int-uint) or [`Decimal*`](/reference/data-types/decimal) or [`Float*`](/reference/data-types/float)
* `[, N]` — Optional. The number of decimal places to round to. Defaults to `0`. [`(U)Int*`](/reference/data-types/int-uint)

**Returned value**

Returns a value rounded by the banker's rounding method. [`(U)Int*`](/reference/data-types/int-uint) or [`Decimal*`](/reference/data-types/decimal) or [`Float*`](/reference/data-types/float)

**Examples**

**Basic usage**

```sql title=Query theme={null}
SELECT number / 2 AS x, roundBankers(x, 0) AS b FROM system.numbers LIMIT 10
```

```response title=Response theme={null}
┌───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 │
└─────┴───┘
```

<h2 id="roundDown">
  roundDown
</h2>

Introduced in: v20.1.0

Rounds a number down to an element in the specified array.
If the value is less than the lower bound, the lower bound is returned.

**Syntax**

```sql theme={null}
roundDown(num, arr)
```

**Arguments**

* `num` — A number to round down. [`(U)Int*`](/reference/data-types/int-uint) or [`Decimal*`](/reference/data-types/decimal) or [`Float*`](/reference/data-types/float)
* `arr` — Array of elements to round `num` down to. [`Array((U)Int*)`](/reference/data-types/array) or [`Array(Float*)`](/reference/data-types/array)

**Returned value**

Returns a number rounded down to an element in `arr`. If the value is less than the lowest bound, the lowest bound is returned. [`(U)Int*`](/reference/data-types/int-uint) or [`Float*`](/reference/data-types/float)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT *, roundDown(*, [3, 4, 5]) FROM system.numbers WHERE number IN (0, 1, 2, 3, 4, 5)
```

```response title=Response theme={null}
┌─number─┬─roundDown(number, [3, 4, 5])─┐
│      0 │                            3 │
│      1 │                            3 │
│      2 │                            3 │
│      3 │                            3 │
│      4 │                            4 │
│      5 │                            5 │
└────────┴──────────────────────────────┘
```

<h2 id="roundDuration">
  roundDuration
</h2>

Introduced in: v1.1.0

Rounds a number down to the closest from a set of commonly used durations: `1, 10, 30, 60, 120, 180, 240, 300, 600, 1200, 1800, 3600, 7200, 18000, 36000`.
If the number is less than one, it returns `0`.

**Syntax**

```sql theme={null}
roundDuration(num)
```

**Arguments**

* `num` — A number to round to one of the numbers in the set of common durations. [`(U)Int*`](/reference/data-types/int-uint) or [`Float*`](/reference/data-types/float)

**Returned value**

Returns `0`, for `num` \< 1. Otherwise, one of: `1, 10, 30, 60, 120, 180, 240, 300, 600, 1200, 1800, 3600, 7200, 18000, 36000`. [`UInt16`](/reference/data-types/int-uint)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT *, roundDuration(*) FROM system.numbers WHERE number IN (0, 9, 19, 47, 101, 149, 205, 271, 421, 789, 1423, 2345, 4567, 9876, 24680, 42573)
```

```response title=Response theme={null}
┌─number─┬─roundDuration(number)─┐
│      0 │                     0 │
│      9 │                     1 │
│     19 │                    10 │
│     47 │                    30 │
│    101 │                    60 │
│    149 │                   120 │
│    205 │                   180 │
│    271 │                   240 │
│    421 │                   300 │
│    789 │                   600 │
│   1423 │                  1200 │
│   2345 │                  1800 │
│   4567 │                  3600 │
│   9876 │                  7200 │
│  24680 │                 18000 │
│  42573 │                 36000 │
└────────┴───────────────────────┘
```

<h2 id="roundToExp2">
  roundToExp2
</h2>

Introduced in: v1.1.0

Rounds a number down to the nearest (whole non-negative) power of two.
If the number is less than one, it returns `0`.

**Syntax**

```sql theme={null}
roundToExp2(num)
```

**Arguments**

* `num` — A number to round. [`(U)Int*`](/reference/data-types/int-uint) or [`Float*`](/reference/data-types/float)

**Returned value**

Returns `num` rounded down to the nearest (whole non-negative) power of two, otherwise `0` for `num < 1`. [`(U)Int*`](/reference/data-types/int-uint) or [`Float*`](/reference/data-types/float)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT *, roundToExp2(*) FROM system.numbers WHERE number IN (0, 2, 5, 10, 19, 50)
```

```response title=Response theme={null}
┌─number─┬─roundToExp2(number)─┐
│      0 │                   0 │
│      2 │                   2 │
│      5 │                   4 │
│     10 │                   8 │
│     19 │                  16 │
│     50 │                  32 │
└────────┴─────────────────────┘
```

<h2 id="trunc">
  trunc
</h2>

Introduced in: v1.1.0

Like [`floor`](#floor) but returns the rounded number with the largest absolute value less than or equal to that of `x`.

**Syntax**

```sql theme={null}
truncate(x[, N])
```

**Aliases**: `truncate`

**Arguments**

* `x` — The value to round. [`Float*`](/reference/data-types/float) or [`Decimal*`](/reference/data-types/decimal) or [`(U)Int*`](/reference/data-types/int-uint)
* `N` — Optional. The number of decimal places to round to. Defaults to zero, which means rounding to an integer. [`(U)Int*`](/reference/data-types/int-uint)

**Returned value**

Returns a rounded number of the same type as `x`. [`Float*`](/reference/data-types/float) or [`Decimal*`](/reference/data-types/decimal) or [`(U)Int*`](/reference/data-types/int-uint)

**Examples**

**Basic usage**

```sql title=Query theme={null}
SELECT truncate(123.499, 1) AS res;
```

```response title=Response theme={null}
┌───res─┐
│ 123.4 │
└───────┘
```
