Operator ‘REAL_TO-/ LREAL-TO’

This IEC operator is used for converting the REAL and LREAL data types into another data type.

REAL_TO_<data type>

LREAL_TO_<data type>

CODESYS rounds the real value of the operand up or down to an integer value and then converts it into the respective type. Exceptions are the STRING, BOOL, REAL, and LREAL data types.

Attention

When converting data types from REAL or LREAL into SINT, USINT, INT, UINT, DINT, UDINT, LINT, or ULINT, if the value of the REAL or LREAL number is outside the value range of of the integer, then you will receive an undefined result depending on the target system. Then an exception is also possible. To obtain code that is independent of the target system, you must use the application to catch value range overflows. If the REAL/LREAL number is within the range, then the conversion operates the same way on all systems.

When converting the the STRING type, please note that the total length of the decimal number is limited to 16 characters. If the (L)REAL number has more decimal places, then the sixteenth place is rounded and represented as a string. If the STRING is defined too short for the number, then CODESYS truncates it from the right.

Attention

Information can be lost when converting from larger data types to smaller data types.

Examples

ST Code Result
i := REAL_TO_INT(1.5); 2
j := REAL_TO_INT(1.4); 1
i := REAL_TO_INT(-1.5); -2
j := REAL_TO_INT(-1.4); -1
FBD Code Result
../_images/3e3133e6cbfd9d5bc0a8640e00bde154 2