Team LiB   Previous Section   Next Section
integer

Allowed coercions

list with one item
string
real

Syntax

set myInt to 12 as integer

Description

An integer value type is a positive or negative number that does not have a decimal part. You can use number as a synonym for integer, but the class of the variable remains integer, as in:

set aNum to 30 as number

If you get the class property of aNum, it will be integer.

If you need a very high number, such as for a variable that will hold the U.S. national debt, you will have to use a real data type. An integer has a range of-536870911 to +536870911. If you're going to work with very high numbers, particularly if the script will increase the value of those numbers, then you should use a real value type to hold the value.

Examples

Here are some examples of numbers that AppleScript will store as integers, as well as numbers that AppleScript will end up storing as reals because they are too big or have a fractional part:

integer class

set bigInt to 500000000

integer class

set bigInt to -500000000

The variable bigInt is stored in a real data type because of the size of the number:

set bigInt to 600000000

The variable bigInt is converted to the real class because it has a fractional part:

set bigInt to 6.1

AppleScript automatically sets a data type for a variable depending on the size of the number or whether it has a decimal point. In the following code, the number starts out as an integer, has a number with a decimal point added to it, then is converted to a real value type to accommodate the fraction:

set int to 1

log class of int -- integer

set int to int + 1.2

log class of int -- real

You cannot depend on AppleScript to always assign the intended value type to a number. This example code will reach the limits of a positive integer (in this case +536870911 on my machine) then begin assigning negative numbers to the int variable. Its class remains integer while it is processed. This was a problem with integer data types in AppleScript 1.3 through 1.4.3 (see the following note).

set int to 536870909

log class of int

repeat 3 times

   set int to int + 1

   log int

   log class of int

end repeat

Changing the opening line of the last example to:

set int to 536870909 as real

will solve the problem and allow the positive number to increase in value by one during each iteration of the loop. (See Chapter 7,for an explanation of repeat loops.)

In AppleScript 1.5 and later, integers will be converted to reals when the integer exceeds 536870911. This obviates the necessity to originally store the incrementing number as a real (but this might be a good idea anyway if you expect the number to grow very large).

    Team LiB   Previous Section   Next Section