

# 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
<a name="athena-duckdb-functions-numeric-ceil"></a>

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](#athena-duckdb-functions-numeric-floor).

### Syntax
<a name="athena-duckdb-functions-numeric-ceil-syntax"></a>

```sql
ceil({{x}})
```

### Arguments
<a name="athena-duckdb-functions-numeric-ceil-arguments"></a>

The `ceil` function takes the following argument:

**{{x}}**  
The number to round up, as a `DECIMAL`, `DOUBLE`, or `FLOAT` value. An integer value is converted to `DOUBLE`.

### Return type
<a name="athena-duckdb-functions-numeric-ceil-return-type"></a>

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
<a name="athena-duckdb-functions-numeric-ceil-examples"></a>

The following SQL query rounds `17.4` up.

```sql
SELECT ceil(17.4) AS result
```

The query returns the following result.


| result | 
| --- | 
| `18` | 

The following SQL query rounds a negative number up, which moves it toward zero.

```sql
SELECT ceil(-17.4) AS result
```

The query returns the following result.


| result | 
| --- | 
| `-17` | 

The following SQL query uses the `ceiling` alias, which returns the same result as `ceil`.

```sql
SELECT ceiling(17.4) AS result
```

The query returns the following result.


| result | 
| --- | 
| `18` | 

The following SQL query passes an integer, so the result is a `DOUBLE`.

```sql
SELECT ceil(17) AS result
```

The query returns the following result.


| result | 
| --- | 
| `17.0` | 

### Considerations
<a name="athena-duckdb-functions-numeric-ceil-considerations"></a>

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
<a name="athena-duckdb-functions-numeric-ceil-differences-from-engine-version-3"></a>

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
<a name="athena-duckdb-functions-numeric-even"></a>

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
<a name="athena-duckdb-functions-numeric-even-syntax"></a>

```sql
even({{x}})
```

### Arguments
<a name="athena-duckdb-functions-numeric-even-arguments"></a>

The `even` function takes the following argument:

**{{x}}**  
The number to round, as a `DOUBLE` value. Values of other numeric types are converted to `DOUBLE`.

### Return type
<a name="athena-duckdb-functions-numeric-even-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-numeric-even-examples"></a>

The following SQL query rounds `2.9` to the next even integer.

```sql
SELECT even(2.9) AS result
```

The query returns the following result.


| result | 
| --- | 
| `4.0` | 

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`.

```sql
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 | 
| --- | --- | 
| `-2.9` | `-4.0` | 
| `0.1` | `2.0` | 
| `2.0` | `2.0` | 
| `3.0` | `4.0` | 

### Considerations
<a name="athena-duckdb-functions-numeric-even-considerations"></a>

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
<a name="athena-duckdb-functions-numeric-floor"></a>

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](#athena-duckdb-functions-numeric-ceil). To remove the fractional part, which moves toward zero for negative numbers, use the [`trunc` function](#athena-duckdb-functions-numeric-trunc).

### Syntax
<a name="athena-duckdb-functions-numeric-floor-syntax"></a>

```sql
floor({{x}})
```

### Arguments
<a name="athena-duckdb-functions-numeric-floor-arguments"></a>

The `floor` function takes the following argument:

**{{x}}**  
The number to round down, as a `DECIMAL`, `DOUBLE`, or `FLOAT` value. An integer value is converted to `DOUBLE`.

### Return type
<a name="athena-duckdb-functions-numeric-floor-return-type"></a>

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
<a name="athena-duckdb-functions-numeric-floor-examples"></a>

The following SQL query rounds `17.4` down.

```sql
SELECT floor(17.4) AS result
```

The query returns the following result.


| result | 
| --- | 
| `17` | 

The following SQL query rounds a negative number down, which moves it away from zero.

```sql
SELECT floor(-17.4) AS result
```

The query returns the following result.


| result | 
| --- | 
| `-18` | 

The following SQL query passes an integer, so the result is a `DOUBLE`.

```sql
SELECT floor(17) AS result
```

The query returns the following result.


| result | 
| --- | 
| `17.0` | 

### Considerations
<a name="athena-duckdb-functions-numeric-floor-considerations"></a>

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
<a name="athena-duckdb-functions-numeric-floor-differences-from-engine-version-3"></a>

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
<a name="athena-duckdb-functions-numeric-round"></a>

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](#athena-duckdb-functions-numeric-round-even). If any argument is `NULL`, the result is `NULL`.

### Syntax
<a name="athena-duckdb-functions-numeric-round-syntax"></a>

```sql
round({{x}} [, {{precision}} ])
```

### Arguments
<a name="athena-duckdb-functions-numeric-round-arguments"></a>

The `round` function takes the following arguments:

**{{x}}**  
The number to round, as a `TINYINT`, `SMALLINT`, `INTEGER`, `BIGINT`, `HUGEINT`, `FLOAT`, `DOUBLE`, or `DECIMAL` value.

**{{precision}}**  
The number of decimal places to keep, as an `INTEGER`. A negative value rounds to tens, hundreds, and so on. The default is `0`. When `x` is a `DECIMAL` value, `precision` must be a constant. This argument is optional.

### Return type
<a name="athena-duckdb-functions-numeric-round-return-type"></a>

The same type as `x`. For a `DECIMAL({{p}}, {{s}})` value, the result is a `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
<a name="athena-duckdb-functions-numeric-round-examples"></a>

The following SQL query rounds a number to two decimal places.

```sql
SELECT round(42.4332, 2) AS result
```

The query returns the following result.


| result | 
| --- | 
| `42.43` | 

The following SQL query uses a negative `precision` to round to the nearest hundred.

```sql
SELECT round(1234.567, -2) AS result
```

The query returns the following result.


| result | 
| --- | 
| `1200` | 

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`.

```sql
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 | 
| --- | --- | 
| `1.01` | `1.0` | 

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`.

```sql
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 | 
| --- | --- | 
| `2` | `3` | 

### Considerations
<a name="athena-duckdb-functions-numeric-round-considerations"></a>

`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
<a name="athena-duckdb-functions-numeric-round-differences-from-engine-version-3"></a>

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
<a name="athena-duckdb-functions-numeric-round-even"></a>

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
<a name="athena-duckdb-functions-numeric-round-even-syntax"></a>

```sql
round_even({{x}}, {{precision}})
```

### Arguments
<a name="athena-duckdb-functions-numeric-round-even-arguments"></a>

The `round_even` function takes the following arguments:

**{{x}}**  
The number to round, as a `TINYINT`, `SMALLINT`, `INTEGER`, `BIGINT`, `HUGEINT`, `FLOAT`, `DOUBLE`, or `DECIMAL` value.

**{{precision}}**  
The number of decimal places to keep, as an `INTEGER`. A negative value rounds to tens, hundreds, and so on. When `x` is a `DECIMAL` value, `precision` must be a constant.

### Return type
<a name="athena-duckdb-functions-numeric-round-even-return-type"></a>

The same type as `x`. For a `DECIMAL({{p}}, {{s}})` value, the result is a `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
<a name="athena-duckdb-functions-numeric-round-even-examples"></a>

The following SQL query rounds a halfway value to the nearest even whole number.

```sql
SELECT round_even(24.5, 0) AS result
```

The query returns the following result.


| result | 
| --- | 
| `24` | 

The following SQL query compares `round` and `round_even` on several halfway values.

```sql
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 | 
| --- | --- | --- | 
| `-2.5` | `-3` | `-2` | 
| `0.5` | `1` | `0` | 
| `1.5` | `2` | `2` | 
| `2.5` | `3` | `2` | 

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`.

```sql
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 | 
| --- | --- | 
| `1300` | `1200` | 

The following SQL query uses the `roundbankers` alias. The nearest even whole number to `25.5` is `26`.

```sql
SELECT roundbankers(25.5, 0) AS result
```

The query returns the following result.


| result | 
| --- | 
| `26` | 

### Considerations
<a name="athena-duckdb-functions-numeric-round-even-considerations"></a>

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
<a name="athena-duckdb-functions-numeric-trunc"></a>

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
<a name="athena-duckdb-functions-numeric-trunc-syntax"></a>

```sql
trunc({{x}} [, {{precision}} ])
```

### Arguments
<a name="athena-duckdb-functions-numeric-trunc-arguments"></a>

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`, or `DECIMAL` value.

**{{precision}}**  
The number of decimal places to keep, as an `INTEGER`. A negative value truncates to tens, hundreds, and so on. The default is `0`. When `x` is a `DECIMAL` value, `precision` must be a constant. This argument is optional.

### Return type
<a name="athena-duckdb-functions-numeric-trunc-return-type"></a>

The same type as `x`. For a `DECIMAL({{p}}, {{s}})` value, the result is a `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
<a name="athena-duckdb-functions-numeric-trunc-examples"></a>

The following SQL query removes the fractional part of a number.

```sql
SELECT trunc(17.4) AS result
```

The query returns the following result.


| result | 
| --- | 
| `17` | 

The following SQL query compares `trunc` and `floor` on a negative number. `trunc` moves toward zero, and `floor` moves down.

```sql
SELECT
    trunc(-17.4) AS trunc_result,
    floor(-17.4) AS floor_result
```

The query returns the following result.


| trunc\_result | floor\_result | 
| --- | --- | 
| `-17` | `-18` | 

The following SQL query keeps two decimal places of one number, and truncates another to hundreds with a negative `precision`.

```sql
SELECT
    trunc(17.4567, 2) AS two_places,
    trunc(1789, -2) AS hundreds
```

The query returns the following result.


| two\_places | hundreds | 
| --- | --- | 
| `17.45` | `1700` | 

### Considerations
<a name="athena-duckdb-functions-numeric-trunc-considerations"></a>

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
<a name="athena-duckdb-functions-numeric-trunc-differences-from-engine-version-3"></a>

Athena engine version 3 names this function `truncate`. DuckDB has no `truncate` function, so replace `truncate({{x}})` and `truncate({{x}}, {{n}})` with `trunc`.

In Athena engine version 3, `truncate({{x}}, {{n}})` of a `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.