trunc
On this page
Truncates the fractional part of a number toward zero.
Syntax
trunc(<expr>)Arguments
| Argument | Type | Description |
|---|---|---|
<expr> | float | Input value. |
Returns
Returns float. Returns NULL if the input is NULL.
Usage notes
For an int or uint column, cast the value to float before calling this function.
Examples
Discard fractional units from signed measurements. Compare truncation with floor when corrections can be negative.
VALUES ('received', 12.8), ('returned', -12.8)
SELECT $0 AS movement, $1 AS measured_units
SELECT
movement,
measured_units,
trunc(measured_units) AS truncated_units,
floor(measured_units) AS floor_units+----------+----------------+-----------------+-------------+
| movement | measured_units | truncated_units | floor_units |
+----------+----------------+-----------------+-------------+
| received | 12.8 | 12.0 | 12.0 |
| returned | -12.8 | -12.0 | -13.0 |
+----------+----------------+-----------------+-------------+Truncation moves toward zero; floor moves toward negative infinity. They differ for the returned quantity.