REAL_TO / LREAL_TO Conversions
IEC operator for conversions from the variable type REAL or LREAL to a different type.
The value will be rounded up or down to the nearest whole number and converted into the new variable type.
Exceptions to this are the following variable types:
oSTRING
oBOOL
oREAL
oLREAL
If a REAL or LREAL is converted to SINT, USINT, INT, UINT, DINT, UDINT, LINT or ULINT and the value of the real number is out of the value range of that integer, the result will be undefined, and may lead to a controller exception.
NOTE: Validate any range overflows by your application and verify that the value of the REAL or LREAL is within the bounds of the target integer before performing the conversion.
When converting to type STRING, consider that the total number of digits is limited to 16. If the (L)REAL number has more digits, then the sixteenth will be rounded. If the length of the STRING is defined too short, it will be cut from the right end.
Examples in ST with conversion results:
Example |
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 |
LD 2.75
REAL_TO_INT
ST i