Truncates a number to the specified number of decimal places.
TRUNC is overloaded in MaxCompute. In the standard data type edition, it is a mathematical function. In the Hive-compatible data type edition, TRUNC is a date function that converts a date value, not a numeric truncation function. For the date behavior, see DATETRUNC. Set the data type edition for your MaxCompute project based on your requirements. For more information, see Data type editions.
Syntax
double|decimal trunc(<number>[, bigint <decimal_places>])
Parameters
| Parameter | Required | Type | Description |
|---|---|---|---|
number |
Yes | DOUBLE or DECIMAL | The value to truncate. STRING and BIGINT inputs are implicitly converted to DOUBLE before truncation. |
decimal_places |
No | BIGINT constant | The decimal position at which to truncate. Defaults to 0 (ones place). A negative value truncates to the left of the decimal point and discards the decimal part. If the absolute value of decimal_places exceeds the length of the integer part, 0 is returned. |
Return value
Returns a DOUBLE or DECIMAL value.
Input type of number |
Return type |
|---|---|
| DOUBLE | DOUBLE |
| DECIMAL | DECIMAL |
| STRING or BIGINT | DOUBLE |
If decimal_places is not a BIGINT constant, an error is returned. If number or decimal_places is null, null is returned.
When the return type is DOUBLE, floating-point representation may cause the displayed value to differ slightly from the mathematically expected result. For example,trunc(125.815, 1)returns125.80000000000001instead of125.8. This is a known behavior in all systems.
Usage notes
The truncated number is padded with zeros from the specified position. For example, trunc(125.815, -1) returns 120.0.
Examples
The examples in this section use the following sample table.
Sample data
create table if not exists mf_math_fun_t(
int_data int,
bigint_data bigint,
double_data double,
decimal_data decimal,
float_data float,
string_data string
);
insert into mf_math_fun_t values
(null, -10, 0.525, 0.525BD, cast(0.525 as float), '10'),
(-20, null, -0.1, -0.1BD, cast(-0.1 as float), '-10'),
(0, -1, null, 20.45BD, cast(-1 as float), '30'),
(-40, 4, 0.89, null, cast(0.89 as float), '-30'),
(5, -50, -1, -1BD, null, '50'),
(-60, 6, 1.5, 1.5BD, cast(1.5 as float), '-50'),
(-1, -70, -7.5, -7.5BD, cast(-7.5 as float), null),
(-80, 1, -10.2, -10.2BD, cast(-10.2 as float), '-1'),
(9, -90, 2.58, 2.58BD, cast(2.58 as float), '0'),
(-100, 10, -5.8, -5.8BD, cast(-5.8 as float), '-90');
Query the table:
select * from mf_math_fun_t;
Result:
+------------+-------------+-------------+--------------+------------+-------------+
| int_data | bigint_data | double_data | decimal_data | float_data | string_data |
+------------+-------------+-------------+--------------+------------+-------------+
| NULL | -10 | 0.525 | 0.525 | 0.525 | 10 |
| -20 | NULL | -0.1 | -0.1 | -0.1 | -10 |
| 0 | -1 | NULL | 20.45 | -1.0 | 30 |
| -40 | 4 | 0.89 | NULL | 0.89 | -30 |
| 5 | -50 | -1.0 | -1 | NULL | 50 |
| -60 | 6 | 1.5 | 1.5 | 1.5 | -50 |
| -1 | -70 | -7.5 | -7.5 | -7.5 | NULL |
| -80 | 1 | -10.2 | -10.2 | -10.2 | -1 |
| 9 | -90 | 2.58 | 2.58 | 2.58 | 0 |
| -100 | 10 | -5.8 | -5.8 | -5.8 | -90 |
+------------+-------------+-------------+--------------+------------+-------------+
Example 1: Default behavior (no scale specified)
When decimal_places is omitted, TRUNC removes all decimal digits, returning the integer part closest to zero.
-- Returns 125.0
select trunc(125.815, 0);
Example 2: Positive scale
A positive decimal_places value keeps that many decimal digits. All examples below use 125.815 as the input.
-- Returns 125.80000000000001 (floating-point representation of 125.8)
select trunc(125.815, 1);
-- Returns 125.81
select trunc(125.815, 2);
-- Returns 125.815
select trunc(125.815, 3);
-- Returns 123.345 (scale exceeds available decimal digits; value is unchanged)
select trunc(123.345, 4);
Example 3: Negative scale
A negative decimal_places value truncates to the left of the decimal point. All examples below use 125.815 as the input.
-- Returns 120.0 (truncated to the tens place)
select trunc(125.815, -1);
-- Returns 100.0 (truncated to the hundreds place)
select trunc(125.815, -2);
-- Returns 0.0 (scale exceeds the integer part length)
select trunc(125.815, -3);
-- Returns 0.0 (scale exceeds the integer part length)
select trunc(123.345, -4);
Example 4: Negative input values
Truncation is towards zero. Compare trunc(-125.815, 2) with trunc(125.815, 2):
-- Returns -125.81 (towards zero, not towards -125.82)
select trunc(-125.815, 2);
Example 5: Null handling
If either argument is null, TRUNC returns null.
-- Returns null
select trunc(123.345, null);
Example 6: Table data
Truncate values in each column of the sample table to one decimal place.
select trunc(bigint_data, 1) as bigint_new,
trunc(double_data, 1) as double_new,
trunc(decimal_data, 1) as decimal_new,
trunc(string_data, 1) as string_new
from mf_math_fun_t;
Result:
+------------+---------------------+-------------+------------+
| bigint_new | double_new | decimal_new | string_new |
+------------+---------------------+-------------+------------+
| -10.0 | 0.5 | 0.5 | 10.0 |
| NULL | -0.1 | -0.1 | -10.0 |
| -1.0 | NULL | 20.4 | 30.0 |
| 4.0 | 0.8 | NULL | -30.0 |
| -50.0 | -1.0 | -1 | 50.0 |
| 6.0 | 1.5 | 1.5 | -50.0 |
| -70.0 | -7.5 | -7.5 | NULL |
| 1.0 | -10.200000000000001 | -10.2 | -1.0 |
| -90.0 | 2.5 | 2.5 | 0.0 |
| 10.0 | -5.800000000000001 | -5.8 | -90.0 |
+------------+---------------------+-------------+------------+
Related functions
TRUNC is a mathematical function. For other mathematical functions, see Mathematical functions.