Truncation · trunc

🟢 11 of 11 forms all ok · VTL 2.2 Reference Manual ↗ · vtlengine 1.10.0rc3 (9e1c81a6) · pandas 2.3.3 · DuckDB 1.5.5
🟢 all ok🟡 partially implemented🛡️ refused with an error🔴 not implemented or crash🔵 engines differ ✅ match · ❌ wrong result · 🛡 guarded error (code) · 💥 crash · 📌 team decision
Dataset formsComponent formsScalar forms
        ---
config:
  flowchart:
    useMaxWidth: false

---
flowchart TB
    classDef ok fill:#d4edda,stroke:#28a745,color:#000
    classDef partial fill:#fff3cd,stroke:#ffc107,color:#000
    classDef refused fill:#d4edda,stroke:#28a745,stroke-width:3px,stroke-dasharray: 2 2,color:#000
    classDef missing fill:#f8d7da,stroke:#dc3545,color:#000
    classDef diverge fill:#cce5ff,stroke:#007bff,color:#000
    classDef block fill:#2b3a4a,stroke:#2b3a4a,color:#fff,font-weight:bold
    classDef ext fill:#fff,stroke:#666,color:#000,stroke-dasharray: 5 5
    O["Truncation · trunc<br/>🟢11"]:::ok
    subgraph G_ds["Dataset"]
        direction LR
        v_ds_digits["trunc(DS, n)"]:::ok
        v_ds_no_digits["trunc(DS)"]:::ok
        v_ds_placeholder["trunc(DS, _)"]:::ok
    end
    subgraph G_comp["Component"]
        direction LR
        v_comp_digits["trunc(comp, n)"]:::ok
        v_comp_no_digits["trunc(comp)"]:::ok
        v_comp_placeholder["trunc(comp, _)"]:::ok
        v_comp_comp_digits["trunc(comp, comp)"]:::ok
        v_sc_comp_digits["trunc(sc, comp)"]:::ok
    end
    subgraph G_sc["Scalar"]
        direction LR
        v_sc_digits["trunc(sc, n)"]:::ok
        v_sc_no_digits["trunc(sc)"]:::ok
        v_sc_placeholder["trunc(sc, _)"]:::ok
    end
    O --- G_ds
    O --- G_comp
    O --- G_sc
    style G_ds fill:#f8f9fa,stroke:#495057,stroke-width:2px
    style G_comp fill:#f8f9fa,stroke:#495057,stroke-width:2px,stroke-dasharray: 8 5
    style G_sc fill:#f8f9fa,stroke:#495057,stroke-width:2px,stroke-dasharray: 2 3
    click v_ds_digits href "#form-ds-digits"
    click v_ds_no_digits href "#form-ds-no-digits"
    click v_ds_placeholder href "#form-ds-placeholder"
    click v_comp_digits href "#form-comp-digits"
    click v_comp_no_digits href "#form-comp-no-digits"
    click v_comp_placeholder href "#form-comp-placeholder"
    click v_comp_comp_digits href "#form-comp-comp-digits"
    click v_sc_comp_digits href "#form-sc-comp-digits"
    click v_sc_digits href "#form-sc-digits"
    click v_sc_no_digits href "#form-sc-no-digits"
    click v_sc_placeholder href "#form-sc-placeholder"
    

Types

Takes number, integer · Gives number, integer

Comes from · number, integer — 44 operators in 6 blocks 🟢44
General purpose operators (2) 🟢2
String operators (3) 🟢3
Numeric operators (17) 🟢17
Time operators (9) 🟢9
Aggregate and Analytic operators (10) 🟢10
Data validation operators (3) 🟢3
Comes from · operators that return any type (their input's) — 37 operators in 8 blocks 🟢37
General purpose operators (5) 🟢5
Join operators (4) 🟢4
Time operators (3) 🟢3
Set operators (4) 🟢4
Hierarchical aggregation (1) 🟢1
Aggregate and Analytic operators (8) 🟢8
Conditional operators (3) 🟢3
Clause operators (9) 🟢9
Goes to · number, integer — 43 operators in 8 blocks 🟢43
General purpose operators (1) 🟢1
String operators (2) 🟢2
Numeric operators (17) 🟢17
Time operators (6) 🟢6
Hierarchical aggregation (1) 🟢1
Aggregate and Analytic operators (10) 🟢10
Data validation operators (2) 🟢2
Definitions (VTL-DL) (4) 🟢4
Goes to · operators that take any scalar — 46 operators in 8 blocks 🟢46
General purpose operators (6) 🟢6
Join operators (4) 🟢4
Comparison operators (11) 🟢11
Set operators (4) 🟢4
Aggregate and Analytic operators (8) 🟢8
Data validation operators (1) 🟢1
Conditional operators (3) 🟢3
Clause operators (9) 🟢9

Forms

trunc(DS, n) 🟢 · number, integer → number

Manual example 1 🟢 pandas ✅ · DuckDB ✅
Tests:

trunc to 0 digits on every measure of DS_1.

Manual:

Truncation, Example 1, output verbatim: “The operator trunc truncates the operand to a number of positions at the right of the decimal point equal to the numDigit parameter.”

Expected:

Me_1 7.5, 7.1, 36.2 and 44.5 give 7.0, 7.0, 36.0 and 44.0; Me_2 5.9, 5.5, 17.7 and 24.3 give 5.0, 5.0, 17.0 and 24.0. Both stay Number, as numDigit is given.

script.vtl
DS_r := trunc(DS_1, 0);
Input DS_1
Id_1Id_2Me_1Me_2
10A7.55.9
10B7.15.5
11A36.217.7
11B44.524.3
Expected DS_r
Id_1Id_2Me_1Me_2
10A7.05.0
10B7.05.0
11A36.017.0
11B44.024.0

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

trunc(DS) 🟢 · number → integer

One number measure 🟢 pandas ✅ · DuckDB ✅
Tests:

trunc with numDigit omitted, on a dataset with one Number measure.

Manual:

Truncation: “The basic scalar type of the result is integer if numDigit is omitted”, and in the Typical behaviours, “Operators changing the data type”, the measure whose type changes is replaced: “A default generic Measure is assigned by default to the result Data Set, depending on the data type of the result”, int_var for an Integer.

Expected:

int_var, an Integer: 3.14159, 2.71828 and 12345.6 give 3, 2 and 12345.

script.vtl
DS_r := trunc(DS_1);
Input DS_1
Id_1Me_1
13.14159
22.71828
312345.6
Expected DS_r
Id_1int_var
13
22
312345

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

Two number measures 🟢 pandas ✅ · DuckDB ✅
Tests:

trunc with numDigit omitted, on a dataset with two Number measures.

Manual:

Truncation: “The basic scalar type of the result is integer if numDigit is omitted”; the result is typed dataset { measure<number> _+ }, and the unary operators give “homonymous Measures in DS_r”.

Expected:

both measures keep their names and become Integers: Me_1 3, 2 and 7, Me_2 12345, 0 and 5. Ceiling and Floor Example 1 do the same, and nothing puts trunc among the “Operators changing the data type”, which allow one measure only.

script.vtl
DS_r := trunc(DS_1);
Input DS_1
Id_1Me_1Me_2
13.1415912345.6
22.718280.4
37.25.9
Expected DS_r
Id_1Me_1Me_2
1312345
220
375

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

trunc(DS, _) 🟢 · number → integer

Placeholder for digits 🟢 pandas ✅ · DuckDB ✅
Tests:

the placeholder _ for numDigit, on a dataset with one Number measure.

Manual:

Truncation: “trunc ( 12345.6, _ ) gives 12345”, and in the Typical behaviours, “Operators changing the data type”, the measure whose type changes is replaced: “A default generic Measure is assigned by default to the result Data Set, depending on the data type of the result”, int_var for an Integer.

Expected:

the placeholder stands for an omitted numDigit, so the result is an Integer, as with trunc(DS_1): int_var 3, 2 and 12345.

script.vtl
DS_r := trunc(DS_1, _);
Input DS_1
Id_1Me_1
13.14159
22.71828
312345.6
Expected DS_r
Id_1int_var
13
22
312345

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

trunc(comp, n) 🟢 · number, integer → number

Manual example 3 🟢 pandas ✅ · DuckDB ✅
Tests:

trunc to a negative number of digits, on a component inside calc.

Manual:

Truncation, Example 3, output verbatim: “If numDigit is negative, the truncation happens at the left of the decimal point.”

Expected:

Me_20 = trunc(Me_1, -1) truncates to tens: 7.5, 7.1, 36.2 and 44.5 give 0.0, 0.0, 30.0 and 40.0; the other components are kept.

script.vtl
DS_r := DS_1[ calc Me_20:= trunc( Me_1 , -1 ) ];
Input DS_1
Id_1Id_2Me_1Me_2
10A7.55.9
10B7.15.5
11A36.217.7
11B44.524.3
Expected DS_r
Id_1Id_2Me_1Me_2Me_20
10A7.55.90.0
10B7.15.50.0
11A36.217.730.0
11B44.524.340.0

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

trunc(comp) 🟢 · number → integer

Manual example 2 🟢 pandas ✅ · DuckDB ✅
Tests:

trunc with numDigit omitted, on a component inside calc.

Manual:

Truncation, Example 2, output verbatim: “The basic scalar type of the result is integer if numDigit is omitted, number otherwise.”

Expected:

Me_10 = trunc(Me_1) is an Integer: 7.5, 7.1, 36.2 and 44.5 give 7, 7, 36 and 44; the other components are kept.

script.vtl
DS_r := DS_1[ calc Me_10:= trunc( Me_1 ) ];
Input DS_1
Id_1Id_2Me_1Me_2
10A7.55.9
10B7.15.5
11A36.217.7
11B44.524.3
Expected DS_r
Id_1Id_2Me_1Me_2Me_10
10A7.55.97
10B7.15.57
11A36.217.736
11B44.524.344

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

trunc(comp, _) 🟢 · number → integer

Placeholder for digits 🟢 pandas ✅ · DuckDB ✅
Tests:

the placeholder _ for numDigit, on a component inside calc.

Manual:

Truncation: “trunc ( 12345.6, _ ) gives 12345”.

Expected:

Me_2 = trunc(Me_1, _) is an Integer, as with numDigit omitted: 3.14159, 2.71828 and 12345.6 give 3, 2 and 12345; the other components are kept.

script.vtl
DS_r := DS_1[calc Me_2 := trunc(Me_1, _)];
Input DS_1
Id_1Me_1
13.14159
22.71828
312345.6
Expected DS_r
Id_1Me_1Me_2
13.1415903
22.7182802
312345.60000012345

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

trunc(comp, comp) 🟢 · number, integer → number

Digits from component 🟢 pandas ✅ · DuckDB ✅
Tests:

numDigit given by an Integer component, which may be negative.

Manual:

Truncation types numDigit as component<integer> | integer, and “trunc( 12345.6, -1 ) gives 12340.0”.

Expected:

Me_3 = trunc(Me_1, Me_2): trunc(3.14159, 2) = 3.14, trunc(2.71828, 3) = 2.718 and trunc(12345.6, -1) = 12340.0, a Number; the other components are kept.

script.vtl
DS_r := DS_1[calc Me_3 := trunc(Me_1, Me_2)];
Input DS_1
Id_1Me_1Me_2
13.141592
22.718283
312345.6-1
Expected DS_r
Id_1Me_1Me_2Me_3
13.14159023.140000
22.71828032.718000
312345.600000-112340.000000

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/DS_r.csv · expected/DS_r.json · example page ↗

trunc(sc, comp) 🟢 · number, integer → number

Digits from component 🟢 pandas ✅ · DuckDB ✅
Tests:

a scalar operand with numDigit given by an Integer component, inside calc.

Manual:

Truncation types numDigit as component<integer> | integer, so it may be a component while the operand is a scalar.

Expected:

the error 1-1-15-8, “At op {op}: {op} operator cannot have a {comp_type} as parameter”: by a team decision, numDigit is a Component only when the operand is a Component too.

script.vtl
DS_r := DS_1[calc Me_2 := trunc(3.14159, Me_1)];
Input DS_1
Id_1Me_1
12
24
30

Expected error 1-1-15-8: At op {op}: {op} operator cannot have a {comp_type} as parameter.

Files: script.vtl · input/DS_1.csv · input/DS_1.json · expected/error.json · example page ↗

trunc(sc, n) 🟢 · number, integer → number

Drops after fourth decimal 🟢 pandas ✅ · DuckDB ✅
Tests:

trunc of a scalar with numDigit given: trunc(SC_1, SC_2) with SC_1 = 3.14159 and SC_2 = 4.

Manual:

Truncation: “trunc ( 3.14159, 4 ) gives 3.1415”.

Expected:

SC_r = 3.1415, a Number: the fourth decimal is kept and the rest dropped.

script.vtl
SC_r := trunc(SC_1, SC_2);
Input scalars
SC_1SC_2
3.141594
Expected scalars

Name

Type

Value

SC_r

Number

"3.141500"

Files: script.vtl · input/scalars.csv · input/scalars.json · expected/scalars.json · example page ↗

trunc(sc) 🟢 · number → integer

Drops fraction to integer 🟢 pandas ✅ · DuckDB ✅
Tests:

trunc of a scalar with numDigit omitted: trunc(SC_1) with SC_1 = 12345.6.

Manual:

Truncation: “trunc ( 12345.6 ) gives 12345”, and “The basic scalar type of the result is integer if numDigit is omitted”.

Expected:

SC_r = 12345, an Integer.

script.vtl
SC_r := trunc(SC_1);
Input scalars
SC_1
12345.6
Expected scalars

Name

Type

Value

SC_r

Integer

"12345"

Files: script.vtl · input/scalars.csv · input/scalars.json · expected/scalars.json · example page ↗

trunc(sc, _) 🟢 · number → integer

Placeholder for digits 🟢 pandas ✅ · DuckDB ✅
Tests:

the placeholder _ for numDigit, on a scalar: trunc(SC_1, _) with SC_1 = 12345.6.

Manual:

Truncation: “trunc ( 12345.6, _ ) gives 12345”.

Expected:

SC_r = 12345, an Integer: the placeholder stands for an omitted numDigit.

script.vtl
SC_r := trunc(SC_1, _);
Input scalars
SC_1
12345.6
Expected scalars

Name

Type

Value

SC_r

Integer

"12345"

Files: script.vtl · input/scalars.csv · input/scalars.json · expected/scalars.json · example page ↗