You are here: Grammar and Syntax > Implicit Conversion

Implicit Conversion

Implicit conversion occurs when operands of differing types are acted upon by an operator. During assignment, the result of the operand on the right will always be implicitly converted to the type of operand on the left of the assignment operator. This table outlines the conversion rules that occur in operations other than assignments:

Expression operands

Implicit conversion of operands

Internal result type

STRING op INTEGER

STRING op STRING

STRING

STRING op DECIMAL

STRING op STRING

STRING

STRING op STRING

STRING op STRING

STRING

INTEGER op INTEGER

INTEGER op INTEGER

*INTEGER DECIMAL

INTEGER op DECIMAL

DECIMAL op DECIMAL

DECIMAL

INTEGER op STRING

INTEGER op INTEGER
or
**DECIMAL op DECIMAL

INTEGER DECIMAL

DECIMAL op INTEGER

DECIMAL op DECIMAL

DECIMAL

DECIMAL op DECIMAL

DECIMAL op DECIMAL

DECIMAL

DECIMAL op STRING

DECIMAL op DECIMAL

DECIMAL

* The result of division between INTEGER data types is always a DECIMAL.

** When a string requires conversion to a numeric value it is converted to a DECIMAL data type if it contains a valid decimal value otherwise, it is converted to an INTEGER data type. The resulting type then determines which implicit conversion rules apply.

Here is an example:

#val=$temp

The value of $temp is converted (internally) to an integer because the assignment is to an integer. During this implicit conversion, the actual value contained in $temp is not changed. If $temp has a value of 10.25 before executing this statement, #val would now have a value of 10.25, and $temp would still be 10.25.

Note Operands of differing types can be assigned to each other, but this does not mean that the two operands will be equal after such assignment.

In this example...

#val="January"

the string constant would be converted to an INTEGER before assignment. Since the string constant does not contain a valid number, the value of #val will be zero (0) after execution of this statement.

In this example...

$temp= 10/6

the constants 10 and 6 are of type INTEGER because they have no decimal value indicated. The resulting internal calculation will be a DECIMAL because the act of division always results in a DECIMAL value. Therefore, the value of $temp after the evaluation will be 1.66667. To assign the integer result of division into a DECIMAL data type, it will be necessary to first assign the result into an INTEGER data type, or to use the expression as the parameter to the INT built-in function.

Here is an example of implicit conversion differences:

TEXT="001";
IF (TEXT=1);
TEMP1="YES";
ELSE;
TEMP1="NO";
END;
IF(1=TEXT);
TEMP2="YES";
ELSE;
TEMP2="NO";
END

After executing these statements, TEMP1 will contain NO and TEMP2 will contain YES.

In the first IF statement, the expression (TEXT=1) compares a string with an integer. According to the rules of implicit conversion, the integer is first converted into a string and then the two objects are evaluated according to the operator. When comparing strings, 001 does not equal 1.

In the second IF statement, the expression (1=TEXT) compares an integer to a string. Implicit conversion will change the string into an integer before performing the operation. The converted expression can be represented as (1=1), which are equal