REAL/LREAL_TO_<type>

This IEC operator converts from the data types REAL and LREAL to a different data type.

Syntax:

REAL_TO_<data type>

LREAL_TO_<data type>

TwinCAT rounds the Real value of the operands up or down to the nearest integer value and converts to the corresponding data type. The exceptions to this are the data types STRING, BOOL, REAL and LREAL.

REAL/LREAL_TO_<type> 1:

If you convert a number of type REAL or LREAL to SINT, USINT, INT, UINT, DINT, UDINT, LINT or ULINT and the value of the REAL/LREAL number is outside the integer's range of values, you get an undefined result. An exception error is also possible!

When converting to the data type STRING, make sure that the total number of decimal places is limited to 16. If the (L)REAL number contains more digits, the sixteenth digit is rounded and then displayed in the string. If the string length defined for the number is too short, TwinCAT truncates it from the right.

REAL/LREAL_TO_<type> 2:

Information may be lost during type conversion from larger to smaller data types.

Samples:

ST code

Result

nVar1 := REAL_TO_INT(1.5);

2

nVar2 := REAL_TO_INT(1.4);

1

nVar1 := REAL_TO_INT(-1.5);

-2

nVar2 := REAL_TO_INT(-1.4);

-1

FBD code

Result

REAL/LREAL_TO_<type> 3:

2

See also: