Conversion of Data Types [AX 2012]
Updated: September 24, 2011
Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012
The automatic and explicit conversions that can be carried out between different data types are listed in the help topics for several of the primitive and composite data types. This topic describes the rules and algorithms that MorphX uses to perform automatic (or implicit) type conversion on primitive data types.
The default values and the internal representation for variables of primitive data types in X++ are shown in the following table.
Data type 
Default 
Internal representation 

Boolean 
false 
Short number 
Integer 
0 
Long number 
Real 
0.0 
BCD (binarycoded digital) number 
Date 
Null 
Date 
String 
empty 
List of characters 
Enums 
0 (first entry) 
Short number 
The internal implementation is shown to explain the automatic type conversions that X++ can perform. The principle is simple: every number can automatically be converted to another number in an expression. Usually, the conversion is upward; that is, from short to long to BCD. The conversion follows the operators in the expression. The following table shows the rules.
Operator 
Description 

+  * 
If one of the operands is a real, the other will be converted into real and the result is a real. If both are integers, Booleans, or enums, no conversion will take place, and the result will be integer, Boolean, or enum, respectively. Otherwise, a Boolean is promoted to enum and enum is promoted to integer 
/ 
Because / is division, the result can be decimal. X++ converts numbers to real before performing the division. The result is real. 
In the examples shown in the following table, which illustrate these principles, the first letter of the data type is used as a variable identifier (b is a Boolean, d is a date, I is an integer, r is a real, and s is a string).
ID 
Expression 
Left data type 
Operands converted 
With numbers 
Result 

1 
i = b + b 
integer 
Boolean, Boolean 
i = false + false 
0 
2 
i = r + b 
integer 
real, real 
i = 33.3 + true 
34 
3 
b = i + r 
Boolean 
real, real 
b = 10 + 33.3 
undefined 
4 
b = i + r 
Boolean 
real, real 
b = 0 + 1 
true 
5 
r = i + b 
real 
integer, integer 
r = 100 + false 
100.0 
6 
r = i + b 
real 
integer, integer 
r = 100 + true 
101.0 
7 
i = r MOD b 
integer 
integer, integer 
i = 33.3 MOD true 
0 
8 
r = i DIV i 
real 
no conversion 
r = 100 DIV 5 
20 
9 
d = d + i 
date 
date, date 
d = 1\1\1998 + 30 
31\1\1998 
10 
i = d + i 
integer 
date, date 
i = 1\1\1998 + 1 
compile error 
11 
d = d + d 
date 
date, date 
d = 1\1\1998+1\1\1998 
compile error 
12 
s = s + s 
string 
string, string 
s = "a" + "b" 
"ab" 
13 
i = s + i 
integer 



Notes for the Preceding Examples
The following table discusses details about some of the rows in the preceding table. The ID values match.
ID 
Discussion 

2 
The assignment i = r + b, with r = 33.3 and b = true is calculated as follows: i = 33.3 + true; // Which is evaluated as... i = 33.3 + 1.0; // Which is evaluated as... i = 34.3; // Which is evaluated as... i = 34; // ...as i is an integer. 
3 
The result is undefined because a Boolean has only two legal values: false (0) and true (1). The result of the expression 10 + 33.3 is 43, which is assigned to the Boolean. Note
Because the internal representation is integer, you can use the Boolean in an expression and it will represent the value 43. The Boolean will be considered true.

9 
Shows that an integer can be added to a date. The system treats the integer as a quantity of days. Note
The utcdatetime data type does not support arithmetic operations and implicit conversions. Instead, methods on the DateTimeUtil class can be used.

10 
Results in a compiler error, because a date cannot be automatically converted into an integer. 
11 
Shows that you cannot add dates together. 
12 
Shows that the + operator strings concatenates two strings to create a new string. 
13 
Shows that there is no automatic conversion between strings and integers (or other numbers). However, you can make explicit conversions by using builtin conversion functions, such as the str2int function. 
Announcements: New book: "Inside Microsoft Dynamics AX 2012 R3" now available. Get your copy at the MS Press Store.