Rounding functions
Round numeric values to specified precision.
Rounding functions provide various ways to adjust numeric values to specified levels of precision.
ClickHouse function reference
floor
Rounds a number down to the nearest integer or to a specified number of decimal places.
Syntax:
Arguments:
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.
Returns:
A rounded number of the same type as x
.
Examples:
Result:
- 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.
This function is useful for rounding prices, measurements, or any decimal values down to the nearest whole number or specified decimal place.
ceiling
Rounds a number up to the nearest integer or to a specified number of decimal places.
Syntax:
Alias:
- ceil
Arguments:
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.
Returns:
The smallest rounded number greater than or equal to x
. The return type is the same as the input type.
Examples:
Result:
Rounding to a specified number of decimal places:
Result:
Rounding to the nearest power of two:
Result:
In this example, if the ‘Carne Asada Supreme’ taco costs 10 (nearest 10).
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:
Alias:
- trunc
Arguments:
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
Returns:
A truncated number of the same type as x
.
Example:
Result:
In this 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:
Arguments:
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.
Returns:
A rounded number of the same type as 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:
Result:
In this 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 Float32
and Float64
inputs, the function uses banker’s rounding when the value is exactly halfway between two numbers. For example:
Result:
Here, 3.5 as a 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).
Arguments:
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
Returns:
A value rounded using banker’s rounding, of the same type as 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)
This function performs banker’s rounding for both floating-point and integer numbers.
Example:
Result:
In this example, 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:
Arguments:
x
(numeric): A number to round. Can be any numeric type.
Returns:
- If
x < 1
, returns 0. - Otherwise, returns
x
rounded down to the nearest power of two. - Return type matches the input type.
Example:
Result:
In this example, 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:
Arguments:
num
(UInt64
orFloat64
): A number to round.
Returns:
- A rounded duration value.
UInt16
.
If the input is less than 1, the function returns 0. Otherwise, it rounds the number down to one of the following commonly used durations (in seconds): 1, 10, 30, 60, 120, 180, 240, 300, 600, 1200, 1800, 3600, 7200, 18000, 36000.
Example:
Result:
In this example, 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:
Arguments:
age
(UInt
orFloat
): A number representing an age in years.
Returns:
A rounded age value based on predefined ranges. (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
Example:
Result:
This function is useful for grouping customers into age brackets for demographic analysis or targeted marketing campaigns in your taco shop.
roundDown
Rounds a number down to the nearest element in a specified array.
Syntax:
Arguments:
num
(numeric): The number to round down.arr
(Array
ofUInt
orFloat
): An array of elements to round down to.
Returns:
The input number rounded down to an element in the array. If the value is less than the lowest bound in the array, the lowest bound is returned. The return type matches the type of elements in arr
.
Example:
Result:
In this 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.
Was this page helpful?