﻿ *_TO_** (IEC 61131-3 Function) - Type Conversion

# Type Conversion

Help Contents

## Introduction

Type Conversion functions convert from one type to another.

You can declare a type conversion three different ways:

FormSyntaxDescription
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 to Integer Conversion

Floating point values (REAL or LREAL) are converted to integer values use the convention outlined in IEC 60559, where:

• The floating point number is rounded to its nearest integer equivalent.
• Halfway cases (for example 1.5) are rounded to the nearest even integer.

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 PointRounded Integer Result
REAL#3.6INT#4 (nearest integer)
REAL#3.5INT#4 (halfway case rounded to the nearest even integer)
REAL#3.4INT#3 (nearest integer)
REAL#2.5INT#2 (halfway case rounded to the nearest even integer)
REAL#1.5INT#2 (halfway case rounded to the nearest even integer)
REAL#-1.5INT#-2 (halfway case rounded to the nearest even integer)
REAL#-2.5INT#-2 (halfway case rounded to the nearest even integer)
REAL#-3.4INT#-3 (nearest integer)

### Invalid Conversions

A type conversion function may generate a runtime error if the From value can not 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.

## Standards Compliance

Table 22.1 - IEC 61131-3 Second Edition.

Table 22.1 - IEC 61131-3 Third Edition.

## Further Information

Elementary Data Type

To learn about the standard elementary data types.

Truncation

To learn about other methods of converting from real numbers to integers.

Conversion Functions

For a summary of all conversion functions.

Common Elements

To learn about other common language elements.

Glossary

For the meaning of terms used in Fernhill SCADA.