ClickHouse function reference
floor
Rounds a number down to the nearest integer or to a specified number of decimal places. Syntax:x
(numeric): The value to round. Can beFloat*
,Decimal*
, or(U)Int*
.N
(numeric, optional): The number of decimal places to round to. Default is 0 (round to integer). Can be negative.
x
.
Examples:
- For integer arguments, when
N
is non-negative, the function returnsx
(does nothing). - If rounding causes an overflow (e.g.,
floor(-128, -1)
), the result is undefined.
ceiling
Rounds a number up to the nearest integer or to a specified number of decimal places. Syntax:- ceil
x
(numeric): The value to round. Can be aFloat32
,Float64
,Decimal*
, orInt*
type.N
(Int
, optional): An integer specifying the number of decimal places to round to. Default is 0.
x
. The return type is the same as the input type.
Examples:
When
N
is negative, the function rounds to the left of the decimal point. For example, ceiling(123.45, -1)
returns 130
.truncate
Rounds a number down to a specified number of decimal places. Syntax:- trunc
x
(numeric): The number to truncate. Can beFloat*
,Decimal*
, or(U)Int*
.N
(Int
, optional): The number of decimal places to keep. Default is 0.- If
N > 0
, truncates to the right of the decimal point. - If
N < 0
, truncates to the left of the decimal point. - If
N = 0
, truncates to an integer.
- If
x
.
Example:
precise_price
shows the taco price truncated to 2 decimal places.rounded_hundreds
rounds down to the nearest hundred.menu_price
displays the taco prices on the menu, truncated to 1 decimal place.
The
truncate
function always rounds towards zero, unlike floor
which rounds down, or ceil
which rounds up.round
Rounds a value to a specified number of decimal places. Syntax:x
(numeric): A number to round. Can beFloat32
,Float64
,Decimal*
, orInt*
.N
(Int*
, optional): The number of decimal places to round to. Defaults to 0.- If N > 0, rounds to the right of the decimal point.
- If N < 0, rounds to the left of the decimal point.
- If N = 0, rounds to the nearest integer.
x
.
Description:
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 Float32
and Float64
inputs and rounds away from zero for other number types (Decimal*
and Int*
).
Example:
round
is used to round taco prices to one decimal place. Note how 2.75 rounds up to 2.8 (away from zero for Decimal
type), while 3.14 rounds down to 3.1.
For Result:Here, 3.5 as a
Float32
and Float64
inputs, the function uses banker’s rounding when the value is exactly halfway between two numbers. For example:Float64
rounds to 4 (banker’s rounding to the nearest even integer), while 3.5 as a Decimal
also rounds to 4 (rounding away from zero).x
(numeric): A number to round. Can beFloat*
,Decimal*
, or(U)Int*
.N
(Int
, optional): The number of decimal places to round to. Optional, defaults to 0.- If
N > 0
, rounds to the right of the decimal point. - If
N < 0
, rounds to the left of the decimal point. - If
N = 0
, rounds to the nearest integer.
- If
x
.
Description:
Banker’s rounding is a method of rounding fractional numbers where ties (numbers exactly halfway between two integers) are rounded to the nearest even integer. This method helps reduce bias in rounding operations.
For example:
- 3.5 rounds to 4
- 2.5 rounds to 2
- 3.55 rounds to 3.6 (with N=1)
- 3.65 rounds to 3.6 (with N=1)
roundBankers
rounds the taco prices to one decimal place using banker’s rounding. Note how 3.25 rounds to 3.2 instead of 3.3.
See Also:
- round function, which uses different rounding rules for non-float types.
roundToExp2
Rounds a number down to the nearest (whole non-negative) power of two. Syntax:x
(numeric): A number to round. Can be any numeric type.
- If
x < 1
, returns 0. - Otherwise, returns
x
rounded down to the nearest power of two. - Return type matches the input type.
roundToExp2
rounds the number of tacos ordered down to the nearest power of two. This could be useful for grouping orders into size categories based on powers of two.
roundDuration
Rounds a number down to a commonly used duration value. Syntax:num
(UInt64
orFloat64
): A number to round.
- A rounded duration value.
UInt16
.
roundDuration
is used to round the order_duration
(in seconds) to the nearest commonly used duration. This can be useful for grouping or categorizing order durations into standardized time intervals.
roundAge
Rounds an age value to predefined age ranges commonly used in demographic analysis. Syntax:age
(UInt
orFloat
): A number representing an age in years.
UInt8
)
- 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
roundDown
Rounds a number down to the nearest element in a specified array. Syntax:num
(numeric): The number to round down.arr
(Array
ofUInt
orFloat
): An array of elements to round down to.
arr
.
Example:
roundDown
rounds each taco_price
to the nearest lower value in the array [3, 4, 5]
. Prices below 3 are rounded to 3, and prices above 5 are rounded to 5.
If the input number is less than the smallest element in the array, the function returns the smallest element. If the array is empty, the function returns 0.