Conversion: REAL, LREAL
Important
If the floating-point number is within the value range of the target data type, then the conversion operates the same way on all systems.
If the floating point number exceeds the range limit, the first bytes of the number are not taken into account.
The operators convert a floating-point number into the specified data types and return a type-converted value. If applicable, the conversion is rounded.
In the case of a REAL_TO_<target type> conversion, the value is first converted to UDINT
and only then cast to the target type.
In the case of the LREAL_TO_<target type> conversion, the value is first converted to ULINT
and only then cast to the target type.
Call syntax: REAL_TO_<target type>(<operand>), LREAL_TO_<target type>(<operand>)
Operand | Type | Description |
---|---|---|
Variable or literal |
|
REAL/LREAL_TO_<target type>
Operator: REAL | Operator: LREAL | Example | Return Value | |
---|---|---|---|---|
|
| REAL_TO___UXINT(1.234) LREAL_TO___UXINT(0.987654321) |
| |
|
| |||
|
| |||
|
| |||
|
| LREAL_TO_BOOL(0.987654321) |
| |
|
| REAL_TO_BYTE(1.234) LREAL_TO_BYTE(0.987654321) |
| |
|
| REAL_TO_DATE(1.234) LREAL_TO_DATE(0.987654321) |
| |
|
| REAL_TO_DINT(1.234) |
| |
|
| REAL_TO_DT(1.234) |
| |
|
| REAL_TO_DWORD(1.234) |
| |
|
| REAL_TO_INT(-1.5) |
| |
|
| REAL_TO_LDATE(1.234) |
| Internally, it is first cast to |
|
| LREAL_TO_DT(0.987654321) |
| Target data type: |
|
| REAL_TO_LINT(1.234) |
| |
|
| |||
| - | REAL_TO_LREAL(1.234) |
| |
|
| Internally, it is first cast to | ||
|
| |||
- |
| |||
|
| |||
|
| REAL_TO_STRING(1.234) |
| |
|
| REAL_TO_TIME(1.234) |
| Internally, it is first cast to ULINT and then the value is returned as in ms. |
|
| REAL_TO_TOD(1.234) |
| Internally, it is first cast to UDINT and then the value is returned as TOD. Syntax: TOD#hh:mm:ss.sss |
|
| |||
|
| |||
|
| |||
|
| |||
|
| REAL_TO_WORD(1.2345678E11) |
| |
|
| REAL_TO_WSTRING(1.23456789E11) |
|
Rounding
When converting to an integer, the operand is rounded up or down to an integer value. For 1 to 4 after the decimal point, the number is rounded down. For 5 to 9, the number is rounded up. Then the rounded number is converted to the specified integer type. If the rounded value is outside of the integer value range, then an undefined, target system-dependent value is returned. An exception error is also possible then.
Converting to a string
For a floating-point number conversion to a string, the number of decimal places of the mantissa is limited to 6. If the number is < 1, then the mantissa is 1 <= m < 10
. If the mantissa has more digits after the comma, then it is rounded to the 6th digit and then converted.
The string variable may also be declared too short for the return value. In this case, the return string is truncated on the right.
