Skip to main content

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

REAL, LREAL

Table 13. Operator: REAL/LREAL_TO_<target type>

Operator: REAL

Operator: LREAL

Example

Return Value

REAL_TO___UXINT

LREAL_TO___UXINT

REAL_TO___UXINT(1.234)
LREAL_TO___UXINT(0.987654321)

1

1

__UXINT, __XINT, __XWORD

REAL_TO___XINT

LREAL_TO___XINT

__UXINT, __XINT, __XWORD

REAL_TO___XWORD

LREAL_TO___XWORD

__UXINT, __XINT, __XWORD

REAL_TO_BIT

LREAL_TO_BIT

BIT

REAL_TO_BOOL

LREAL_TO_BOOL

LREAL_TO_BOOL(0.987654321)

TRUE

BOOL

REAL_TO_BYTE

LREAL_TO_BYTE

REAL_TO_BYTE(1.234)
LREAL_TO_BYTE(0.987654321)

1

Integer

REAL_TO_DATE

LREAL_TO_DATE

REAL_TO_DATE(1.234)
LREAL_TO_DATE(0.987654321)

D#1970-1-1

Data type: DATE

REAL_TO_DINT

LREAL_TO_DINT

REAL_TO_DINT(1.234)

1

Integer

REAL_TO_DT

LREAL_TO_DT

REAL_TO_DT(1.234)

D#1970-1-1-0:0:1

Data type: DATE

REAL_TO_DWORD

LREAL_TO_DWORD

REAL_TO_DWORD(1.234)

32#1

Integer

REAL_TO_INT

LREAL_TO_INT

REAL_TO_INT(-1.5)

-2

Integer

REAL_TO_LDATE

LREAL_TO_LDATE

REAL_TO_LDATE(1.234)

DT#1970-1-1

Internally, it is first cast to ULINT and then the value is returned as a date.

REAL_TO_LDT

LREAL_TO_LDT

LREAL_TO_DT(0.987654321)

D#1970-1-1-0:0:1

Target data type: LDATE_AND_TIME

Data type: DATE

REAL_TO_LINT

LREAL_TO_LINT

REAL_TO_LINT(1.234)

1

Integer

REAL_TO_LTOD

LREAL_TO_LTOD

Data type: DATE

REAL_TO_LREAL

-

REAL_TO_LREAL(1.234)

1.234

REAL, LREAL

REAL_TO_LTIME

LREAL_TO_LTIME

Internally, it is first cast to ULINT and then the value is returned as in ns.

Data type: TIME, LTIME

REAL_TO_LWORD

LREAL_TO_LWORD

Integer

-

LREAL_TO_REAL

REAL_TO_SINT

LREAL_TO_SINT

Integer

REAL_TO_STRING

LREAL_TO_STRING

REAL_TO_STRING(1.234)

'1.234'

STRING

REAL_TO_TIME

LREAL_TO_TIME

REAL_TO_TIME(1.234)

T#1ms

Internally, it is first cast to ULINT and then the value is returned as in ms.

Data type: TIME, LTIME

REAL_TO_TOD

LREAL_TO_TOD

REAL_TO_TOD(1.234)

TOD#0:0:0.001

Internally, it is first cast to UDINT and then the value is returned as TOD.

Syntax: TOD#hh:mm:ss.sss

Data type: DATE

REAL_TO_UDINT

LREAL_TO_UDINT

Integer

REAL_TO_UINT

LREAL_TO_UINT

Integer

REAL_TO_ULINT

LREAL_TO_ULINT

Integer

REAL_TO_USINT

LREAL_TO_USINT

Integer

REAL_TO__WORD

LREAL_TO__WORD

REAL_TO_WORD(1.2345678E11)

1

Integer

REAL_TO_WSTRING

LREAL_TO_WSTRING

REAL_TO_WSTRING(1.23456789E11)

"1.234"

WSTRING



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.

Example 170. Example
_cds_img_fbd_convert_real_to_string.png