Type Conversion functions convert from one type to another.
You can declare a type conversion three different ways:
Form | Syntax | Description |
---|---|---|
Exactly Typed | *_TO_** |
Where * is the type to change from, and ** is the type to change to. For example: REAL_TO_INT( 12.5 ) |
Overloaded #1 | ANY_TO_** |
Where ** is the type to change to. For example: ANY_TO_INT( 12.5 ) |
Overloaded #2 | TO_** |
Where ** is the type to change to. For example: TO_INT( 12.5 ) |
The from (*) and to (**) types can be any Elementary Data Type.
Floating point values (REAL or LREAL) are converted to integer values use the convention outlined in IEC 60559, where:
This method of rounding is sometimes called statistician's rounding, Dutch rounding, or banker's rounding. It has the advantage of minimizing the error when calculating the sum of a set of rounded values.
Examples of floating point rounding using REAL_TO_INT( X ):
Floating Point | Rounded Integer Result |
---|---|
REAL#3.6 | INT#4 (nearest integer) |
REAL#3.5 | INT#4 (halfway case rounded to the nearest even integer) |
REAL#3.4 | INT#3 (nearest integer) |
REAL#2.5 | INT#2 (halfway case rounded to the nearest even integer) |
REAL#1.5 | INT#2 (halfway case rounded to the nearest even integer) |
REAL#-1.5 | INT#-2 (halfway case rounded to the nearest even integer) |
REAL#-2.5 | INT#-2 (halfway case rounded to the nearest even integer) |
REAL#-3.4 | INT#-3 (nearest integer) |
A type conversion function may generate a runtime error if the From value cannot be converted to the To type. For example:
A := STRING_TO_INT( 'Not a valid integer' );
The above code would generate a runtime type conversion error.
IEC 61131-3 Second Edition: Table 22.1.
IEC 61131-3 Third Edition: Table 22.1.
To learn about the standard elementary data types.
TruncationTo learn about other methods of converting from real numbers to integers.
For a summary of all conversion functions.
To learn about other common language elements.
For the meaning of terms used in Fernhill SCADA.