Rounding
Rounding functions reference.
ceil
Like 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
ceiling(x[, N])Arguments
xβ The value to round.Float*orDecimal*or(U)Int*Nβ Optional. The number of decimal places to round to. Defaults to zero, which means rounding to an integer. Can be negative.(U)Int*
Returned value
Returns a rounded number of the same type as x. Float* or Decimal* or (U)Int*
Examples
Basic usage
SELECT ceiling(123.45, 1) AS roundedββroundedββ
β 123.5 β
βββββββββββNegative precision
SELECT ceiling(123.45, -1)ββceiling(123.45, -1)ββ
β 130 β
βββββββββββββββββββββββIntroduced in version 1.1.
floor
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
floor(x[, N])Arguments
xβ The value to round.Float*orDecimal*or(U)Int*Nβ Optional. The number of decimal places to round to. Defaults to zero, which means rounding to an integer. Can be negative.(U)Int*
Returned value
Returns a rounded number of the same type as x. Float* or Decimal* or (U)Int*
Examples
Usage example
SELECT floor(123.45, 1) AS roundedββroundedββ
β 123.4 β
βββββββββββNegative precision
SELECT floor(123.45, -1)ββfloor(123.45, -1)ββ
β 120 β
βββββββββββββββββββββIntroduced in version 1.1.
round
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
round(x[, N])Arguments
xβ A number to round.Float*orDecimal*or(U)Int*Nβ Optional. The number of decimal places to round to. Defaults to0.(U)Int*
Returned value
Returns a rounded number of the same type as x. Float* or Decimal* or (U)Int*
Examples
Float inputs
SELECT number / 2 AS x, round(x) FROM system.numbers LIMIT 3;ββββxββ¬βround(x)ββ
β 0 β 0 β
β 0.5 β 0 β
β 1 β 1 β
βββββββ΄βββββββββββDecimal inputs
SELECT cast(number / 2 AS Decimal(10,4)) AS x, round(x) FROM system.numbers LIMIT 3;ββββxββ¬βround(x)ββ
β 0 β 0 β
β 0.5 β 1 β
β 1 β 1 β
βββββββ΄βββββββββββIntroduced in version 1.1.
roundAge
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, forage < 1. - Returns
17, for1 β€ age β€ 17. - Returns
18, for18 β€ age β€ 24. - Returns
25, for25 β€ age β€ 34. - Returns
35, for35 β€ age β€ 44. - Returns
45, for45 β€ age β€ 54. - Returns
55, forage β₯ 55.
Syntax
roundAge(num)Arguments
Returned value
Returns either the highest or lowest age of the range age falls within. UInt8
Examples
Usage example
SELECT *, roundAge(*) FROM system.numbers WHERE number IN (0, 5, 20, 31, 37, 54, 72);ββnumberββ¬βroundAge(number)ββ
β 0 β 0 β
β 5 β 17 β
β 20 β 18 β
β 31 β 25 β
β 37 β 35 β
β 54 β 45 β
β 72 β 55 β
ββββββββββ΄βββββββββββββββββββIntroduced in version 1.1.
roundBankers
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.5rounds up to4,2.5rounds down to2. - The
roundfunction performs the same rounding for floating point numbers. - The
roundBankersfunction also rounds integers the same way, for example,roundBankers(45, -1) = 40. - In other cases, the function rounds numbers to the nearest integer. :::
:::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. :::
Syntax
roundBankers(x[, N])Arguments
xβ A number to round.(U)Int*orDecimal*orFloat*[, N]β Optional. The number of decimal places to round to. Defaults to0.(U)Int*
Returned value
Returns a value rounded by the banker's rounding method. (U)Int* or Decimal* or Float*
Examples
Basic usage
SELECT number / 2 AS x, roundBankers(x, 0) AS b FROM system.numbers LIMIT 10ββββ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 β
βββββββ΄ββββIntroduced in version 20.1.
roundDown
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
roundDown(num, arr)Arguments
numβ A number to round down.(U)Int*orDecimal*orFloat*arrβ Array of elements to roundnumdown to.Array((U)Int*)orArray(Float*)
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* or Float*
Examples
Usage example
SELECT *, roundDown(*, [3, 4, 5]) FROM system.numbers WHERE number IN (0, 1, 2, 3, 4, 5)ββnumberββ¬βroundDown(number, [3, 4, 5])ββ
β 0 β 3 β
β 1 β 3 β
β 2 β 3 β
β 3 β 3 β
β 4 β 4 β
β 5 β 5 β
ββββββββββ΄βββββββββββββββββββββββββββββββIntroduced in version 20.1.
roundDuration
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
roundDuration(num)Arguments
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
Examples
Usage example
SELECT *, roundDuration(*) FROM system.numbers WHERE number IN (0, 9, 19, 47, 101, 149, 205, 271, 421, 789, 1423, 2345, 4567, 9876, 24680, 42573)ββ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 β
ββββββββββ΄ββββββββββββββββββββββββIntroduced in version 1.1.
roundToExp2
Rounds a number down to the nearest (whole non-negative) power of two.
If the number is less than one, it returns 0.
Syntax
roundToExp2(num)Arguments
Returned value
Returns num rounded down to the nearest (whole non-negative) power of two, otherwise 0 for num < 1. (U)Int* or Float*
Examples
Usage example
SELECT *, roundToExp2(*) FROM system.numbers WHERE number IN (0, 2, 5, 10, 19, 50)ββnumberββ¬βroundToExp2(number)ββ
β 0 β 0 β
β 2 β 2 β
β 5 β 4 β
β 10 β 8 β
β 19 β 16 β
β 50 β 32 β
ββββββββββ΄ββββββββββββββββββββββIntroduced in version 1.1.
trunc
Like floor but returns the rounded number with the largest absolute value less than or equal to that of x.
Syntax
truncate(x[, N])Arguments
xβ The value to round.Float*orDecimal*or(U)Int*Nβ Optional. The number of decimal places to round to. Defaults to zero, which means rounding to an integer.(U)Int*
Returned value
Returns a rounded number of the same type as x. Float* or Decimal* or (U)Int*
Examples
Basic usage
SELECT truncate(123.499, 1) AS res;ββββresββ
β 123.4 β
βββββββββIntroduced in version 1.1.