Rounding functions
Use these functions to round a number up, down, or to a given number of decimal places, or to drop digits without rounding. The functions differ in how they handle halfway values and negative numbers.
ceil function
Returns the smallest integer value that is greater than or equal to x. For a negative number, rounding up moves toward zero, so ceil(-17.4) returns -17. If x is NULL, the result is NULL.
ceiling is an alias of ceil. To round down, use the floor function.
Syntax
ceil(x)
Arguments
The ceil function takes the following argument:
x-
The number to round up, as a
DECIMAL,DOUBLE, orFLOATvalue. An integer value is converted toDOUBLE.
Return type
The same type as x for DOUBLE and FLOAT. For DECIMAL, a DECIMAL with the same precision and a scale of 0. For example, the literal 17.4 is DECIMAL(3,1), so ceil(17.4) returns DECIMAL(3,0). For an integer type, DOUBLE.
Examples
The following SQL query rounds 17.4 up.
SELECT ceil(17.4) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query rounds a negative number up, which moves it toward zero.
SELECT ceil(-17.4) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query uses the ceiling alias, which returns the same result as ceil.
SELECT ceiling(17.4) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query passes an integer, so the result is a DOUBLE.
SELECT ceil(17) AS result
The query returns the following result.
| result |
|---|
|
Considerations
An integer argument returns a DOUBLE, so ceil(17) returns 17.0. If you need an integer type, cast the result, for example CAST(ceil(.x) AS BIGINT)
Differences from Athena engine version 3
In Athena engine version 3, ceil of an INTEGER or BIGINT value returns the same integer type. In DuckDB, it returns DOUBLE. To keep an integer type, cast the result, for example CAST(ceil(.x) AS BIGINT)
even function
Returns the even integer closest to x that is at least as far from zero as x. A positive value is rounded up and a negative value is rounded down, so even(2.9) returns 4.0 and even(-2.9) returns -4.0. A value that is already an even integer is returned unchanged. If x is NULL, the result is NULL.
even is not the same as rounding half to even. It always moves to an even integer, including for a value such as 0.1, which becomes 2.0.
Syntax
even(x)
Arguments
The even function takes the following argument:
x-
The number to round, as a
DOUBLEvalue. Values of other numeric types are converted toDOUBLE.
Return type
DOUBLE
Examples
The following SQL query rounds 2.9 to the next even integer.
SELECT even(2.9) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query rounds several values. 2.0 is already even and is returned unchanged, and -2.9 is rounded away from zero to -4.
SELECT x AS input, even(x) AS result FROM (VALUES (2.0), (3.0), (-2.9), (0.1)) AS v(x) ORDER BY x
The query returns the following results.
| input | result |
|---|---|
|
|
|
|
|
|
|
|
Considerations
The result is always a DOUBLE, even for an integer argument, so even(5) returns 6.0. If you need an integer type, cast the result.
floor function
Returns the largest integer value that is less than or equal to x. For a negative number, rounding down moves away from zero, so floor(-17.4) returns -18. If x is NULL, the result is NULL.
To round up, use the ceil function. To remove the fractional part, which moves toward zero for negative numbers, use the trunc function.
Syntax
floor(x)
Arguments
The floor function takes the following argument:
x-
The number to round down, as a
DECIMAL,DOUBLE, orFLOATvalue. An integer value is converted toDOUBLE.
Return type
The same type as x for DOUBLE and FLOAT. For DECIMAL, a DECIMAL with the same precision and a scale of 0, for example DECIMAL(3,0) for the literal 17.4, which is DECIMAL(3,1). For an integer type, DOUBLE.
Examples
The following SQL query rounds 17.4 down.
SELECT floor(17.4) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query rounds a negative number down, which moves it away from zero.
SELECT floor(-17.4) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query passes an integer, so the result is a DOUBLE.
SELECT floor(17) AS result
The query returns the following result.
| result |
|---|
|
Considerations
An integer argument returns a DOUBLE, so floor(17) returns 17.0. If you need an integer type, cast the result, for example CAST(floor(.x) AS BIGINT)
Differences from Athena engine version 3
In Athena engine version 3, floor of an INTEGER or BIGINT value returns the same integer type. In DuckDB, it returns DOUBLE. To keep an integer type, cast the result, for example CAST(floor(.x) AS BIGINT)
round function
Returns x rounded to precision decimal places. If you omit precision, the function rounds to the nearest whole number. A negative precision rounds to the left of the decimal point, so -2 rounds to the nearest hundred.
A value exactly halfway between two results is rounded away from zero, so 2.5 becomes 3 and -2.5 becomes -3. To round halfway values to the nearest even digit instead, use the round_even function. If any argument is NULL, the result is NULL.
Syntax
round(x [, precision ])
Arguments
The round function takes the following arguments:
x-
The number to round, as a
TINYINT,SMALLINT,INTEGER,BIGINT,HUGEINT,FLOAT,DOUBLE, orDECIMALvalue. precision-
The number of decimal places to keep, as an
INTEGER. A negative value rounds to tens, hundreds, and so on. The default is0. Whenxis aDECIMALvalue,precisionmust be a constant. This argument is optional.
Return type
The same type as x. For a DECIMAL( value, the result is a p, s)DECIMAL with the same p. Its scale is lowered to precision when precision is less than s, or to 0 when you omit precision or it is negative. For example, round(42.4332, 2) returns DECIMAL(6,2).
Examples
The following SQL query rounds a number to two decimal places.
SELECT round(42.4332, 2) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query uses a negative precision to round to the nearest hundred.
SELECT round(1234.567, -2) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query rounds the same number as a DECIMAL and as a DOUBLE. The DOUBLE value is stored as slightly less than 1.005, so it rounds down to 1.0.
SELECT round(1.005, 2) AS decimal_result, round(CAST(1.005 AS DOUBLE), 2) AS double_result
The query returns the following result.
| decimal_result | double_result |
|---|---|
|
|
The following SQL query converts the DOUBLE value 2.5e0, which is halfway between 2 and 3, to INTEGER in two ways. The cast alone rounds it to the even number 2. Calling round before the cast rounds it away from zero, to 3.
SELECT CAST(2.5e0 AS INTEGER) AS cast_result, CAST(round(2.5e0) AS INTEGER) AS round_result
The query returns the following result.
| cast_result | round_result |
|---|---|
|
|
Considerations
FLOAT and DOUBLE values are binary fractions, so many decimal numbers, such as 1.005, are stored as a value that is slightly lower or higher. For example, round(CAST(1.005 AS DOUBLE), 2) returns 1.0, while round(1.005, 2) on the DECIMAL literal returns 1.01. To round exact decimal amounts, such as money, use DECIMAL values.
A cast from DOUBLE or FLOAT to an integer type rounds halfway values to the even number, so CAST(2.5e0 AS INTEGER) returns 2. To round halfway values away from zero, call round first, as in CAST(round(.x) AS INTEGER)
When x is a DECIMAL value, precision must be a constant, and a precision taken from a column makes the query fail. Integer, FLOAT, and DOUBLE values accept a precision from a column.
Differences from Athena engine version 3
In Athena engine version 3, round of a DECIMAL value keeps the scale of the input, so round(CAST(42.4332 AS DECIMAL(6, 4)), 2) returns 42.4300. In DuckDB, the result keeps only precision digits after the decimal point, so the same call returns 42.43. The two values are equal, but the result type and its text form differ.
round_even function
Returns x rounded to precision decimal places, as round does, except for values exactly halfway between two results. The round_even function rounds those to the result whose last digit is even, so 2.5 becomes 2, 3.5 becomes 4, and -2.5 becomes -2. This method is also called banker's rounding. Because halfway values round up as often as they round down, the total of many rounded values stays closer to the total of the original values.
A negative precision rounds to the left of the decimal point. If any argument is NULL, the result is NULL.
roundbankers is an alias of round_even.
Syntax
round_even(x, precision)
Arguments
The round_even function takes the following arguments:
x-
The number to round, as a
TINYINT,SMALLINT,INTEGER,BIGINT,HUGEINT,FLOAT,DOUBLE, orDECIMALvalue. precision-
The number of decimal places to keep, as an
INTEGER. A negative value rounds to tens, hundreds, and so on. Whenxis aDECIMALvalue,precisionmust be a constant.
Return type
The same type as x. For a DECIMAL( value, the result is a p, s)DECIMAL with the same p. Its scale is lowered to precision when precision is less than s, or to 0 when precision is negative. For example, round_even(24.5, 0) returns DECIMAL(3,0).
Examples
The following SQL query rounds a halfway value to the nearest even whole number.
SELECT round_even(24.5, 0) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query compares round and round_even on several halfway values.
SELECT x, round(x, 0) AS round_result, round_even(x, 0) AS round_even_result FROM (VALUES (-2.5), (0.5), (1.5), (2.5)) AS v(x) ORDER BY x
The query returns the following results.
| x | round_result | round_even_result |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
The following SQL query rounds an integer to the nearest hundred. 1250 is halfway between 1200 and 1300, so round returns 1300 and round_even returns 1200.
SELECT round(1250, -2) AS round_result, round_even(1250, -2) AS round_even_result
The query returns the following result.
| round_result | round_even_result |
|---|---|
|
|
The following SQL query uses the roundbankers alias. The nearest even whole number to 25.5 is 26.
SELECT roundbankers(25.5, 0) AS result
The query returns the following result.
| result |
|---|
|
Considerations
Unlike round, round_even has no one-argument form, so a call such as round_even(24.5) fails. To round to a whole number, pass 0 as precision.
A DOUBLE value that looks like a halfway value is often stored as slightly less or slightly more than that value, so the even-digit rule does not apply to it. For example, round_even(2.675, 2) on the DECIMAL literal returns 2.68, but round_even(CAST(2.675 AS DOUBLE), 2) returns 2.67. Use DECIMAL values when the halfway rule matters.
trunc function
Returns x with the digits after precision decimal places removed. The function does not round. It moves the value toward zero, so trunc(17.9) returns 17 and trunc(-17.9) returns -17. If you omit precision, the function removes the whole fractional part. A negative precision also sets digits to the left of the decimal point to zero, so -2 truncates to hundreds.
If any argument is NULL, the result is NULL.
Syntax
trunc(x [, precision ])
Arguments
The trunc function takes the following arguments:
x-
The number to truncate, as a value of any integer type, signed or unsigned, or a
FLOAT,DOUBLE, orDECIMALvalue. precision-
The number of decimal places to keep, as an
INTEGER. A negative value truncates to tens, hundreds, and so on. The default is0. Whenxis aDECIMALvalue,precisionmust be a constant. This argument is optional.
Return type
The same type as x. For a DECIMAL( value, the result is a p, s)DECIMAL with the same p. Its scale is lowered to precision when precision is less than s, or to 0 when you omit precision or it is negative. For example, trunc(17.4) returns DECIMAL(3,0).
Examples
The following SQL query removes the fractional part of a number.
SELECT trunc(17.4) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query compares trunc and floor on a negative number. trunc moves toward zero, and floor moves down.
SELECT trunc(-17.4) AS trunc_result, floor(-17.4) AS floor_result
The query returns the following result.
| trunc_result | floor_result |
|---|---|
|
|
The following SQL query keeps two decimal places of one number, and truncates another to hundreds with a negative precision.
SELECT trunc(17.4567, 2) AS two_places, trunc(1789, -2) AS hundreds
The query returns the following result.
| two_places | hundreds |
|---|---|
|
|
Considerations
For negative numbers that have a fractional part, trunc and floor return different results: trunc moves toward zero, and floor moves down.
trunc does not accept DATE or TIMESTAMP values, and such a call fails. Use date_trunc instead.
When x is a DECIMAL value, precision must be a constant, and a precision taken from a column makes the query fail.
Differences from Athena engine version 3
Athena engine version 3 names this function truncate. DuckDB has no truncate function, so replace truncate( and x)truncate( with x, n)trunc.
In Athena engine version 3, truncate( of a x, n)DECIMAL value keeps the scale of the input, so truncate(CAST(2.759 AS DECIMAL(4, 3)), 2) returns 2.750. In DuckDB, trunc lowers the scale to n, so trunc(CAST(2.759 AS DECIMAL(4, 3)), 2) returns 2.75. The two values are equal, but the result type and its text form differ.