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

IN

ANY_REAL

The real value to truncate.

Outputs

Name

Type

Description

Return value

ANY_INT

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) returns 3

  • TRUNC(-3.7) returns -3

  • TRUNC(0.9) returns 0

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

References