TRUNC¶
Truncates a real (floating-point) value toward zero, removing the fractional part and returning an integer.
Signature¶
┌─────────┐
IN ─┤ TRUNC ├─ OUT
└─────────┘
FUNCTION TRUNC : ANY_INT
VAR_INPUT
IN : ANY_REAL;
END_VAR
END_FUNCTION
TRUNC accepts REAL or LREAL for IN. The return type
is determined by the variable being assigned to: SINT, INT,
DINT, or LINT.
Inputs
Name |
Type |
Description |
|---|---|---|
|
|
The real value to truncate. |
Outputs
Name |
Type |
Description |
|---|---|---|
Return value |
|
IN with its fractional part removed. The integer type is determined by the assignment target. |
Description¶
TRUNC removes the fractional part of a real number, truncating toward
zero. This means positive values are rounded down and negative values
are rounded up (toward zero).
TRUNC(3.7)returns3TRUNC(-3.7)returns-3TRUNC(0.9)returns0
The return type is determined by the variable being assigned to.
Example¶
result := TRUNC(REAL#3.7); (* result = 3 *)
neg_result := TRUNC(REAL#-3.7); (* neg_result = -3 *)
See Also¶
Type Conversions — explicit type conversion functions
ABS — absolute value
References¶
IEC 61131-3 §2.5.1.5.2