Integers

Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012

Integers are numbers without decimals. X++ has two integer types:

  • int, which is 32 bits wide

  • int64, which is 64 bits wide

Integers are used especially as control variables in repetitive statements or as indexes in arrays.

Range and Precision

The range of an int is: [-2,147,483,647 : 2,147,483,647].

The range of an int64 is: [-9,223,372,036,854,775,808 : 9,223,372,036,854,775,808]

Declaring Integers

integer declaration

=

int | int64 Variable { , Variable } ;

variable

=

identifier [ option ]

option

=

arrayoptions | initialization

    // Declaration of an integer variable, i
    int i;
    // Declaration of two int64 variables
    int64 i1, i2;
    // An integer variable is initialized to the value 100
    int i3 = 100;
    // Declaration of a dynamic array of integers
    int i4[];

Warning

If you try to assign the largest integer plus 1 to an int64, you will get the wrong result. This is because it is interpreted as a 32-bit number, and therefore the number is wrapped round and stored as -2,147,483,647 instead. To prevent this, add a "u" to the end of the number, for example: int64 i = 0x8000 0000u (0x8000 0000 is 2,147,483,648).

Integer Literals

You can use integer literals anywhere an integer-expression is expected. An integer literal is the integer written directly in the code—for instance, 32768. The range for integers is shown previously, and all integers in this range can be used as literals in X++.

Automatic Conversions

Integers are automatically converted into boolean, enum, and real.

Using Integers in Expressions

Integers can be used in all expressions, and relational operators and arithmetic operators can be applied. For example:

    void element() 
    {
        // Two integer variables are declared and initialized
        int i = 1, j = 2;
        ;
     
        // j is assigned the result of j + ((i + i) DIV 2)
        j +=(i + i) div 2;
     
        // This results in: j=3
        if (j>2)
        { 
            print "J is greater than 2";
        }
        else 
        {
            print "J is NOT greater than 2";
        }
    }

Overview of Integers

Keywords

int, int64

Size

32 bits or 64 bits

Scope of data type

[-2,147,483,648 : 2,147,483,647] or

[-9,223,372,036,854,775,808 : 9,223,372,036,854,775,808]

Default value

0

Implicit conversions

Automatically converted to real, boolean, and enum

Explicit conversions

str2int, int2str, str2int64, int642str

See also

Data Types in X++

Variables

str2Int Function

int2Str Function

str2Int64 Function

int642Str Function

Announcements: New book: "Inside Microsoft Dynamics AX 2012 R3" now available. Get your copy at the MS Press Store.