PackageTop Level
Classpublic final class int
Inheritanceint Inheritance Object

Language version: ActionScript 3.0
Runtime version: 

The int class lets you work with the data type representing a 32-bit signed integer. The range of values represented by the int class is -2,147,483,648 (-2^31) to 2,147,483,647 (2^31-1).

The constant properties of the int class, MAX_VALUE and MIN_VALUE, are static, which means that you don't need an object to use them, so you don't need to use the constructor. The methods, however, are not static, which means that you do need an object to use them. You can create an int object by using the int class constructor or by declaring a variable of type int and assigning the variable a literal value.

The int data type is useful for loop counters and other situations where a floating point number is not needed, and is similar to the int data type in Java and C++. The default value of a variable typed as int is 0

If you are working with numbers that exceed int.MAX_VALUE, consider using Number.

The following example calls the toString() method of the int class, which returns the string 1234:


 var myint:int = 1234;

 myint.toString();

 

The following example assigns the value of the MIN_VALUE property to a variable declared without the use of the constructor:


 var smallest:int = int.MIN_VALUE;

 

View the examples.

See also

uint
Number


Public Properties
 PropertyDefined by
 Inheritedconstructor : Object
A reference to the class object or constructor function for a given object instance.
Object
 Inheritedprototype : Object
[static] A reference to the prototype object of a class or function object.
Object
Public Methods
 MethodDefined by
  
int(num:Object)
Constructor; creates a new int object.
int
 Inherited
Indicates whether an object has a specified property defined.
Object
 Inherited
Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter.
Object
 Inherited
Indicates whether the specified property exists and is enumerable.
Object
 Inherited
Sets the availability of a dynamic property for loop operations.
Object
  
toExponential(fractionDigits:uint):String
Returns a string representation of the number in exponential notation.
int
  
toFixed(fractionDigits:uint):String
Returns a string representation of the number in fixed-point notation.
int
 Inherited
Returns the string representation of this object, formatted according to locale-specific conventions.
Object
  
toPrecision(precision:uint):String
Returns a string representation of the number either in exponential notation or in fixed-point notation.
int
  
Returns the string representation of an int object.
int
  
Returns the primitive value of the specified int object.
int
Public Constants
 ConstantDefined by
  MAX_VALUE : int = 2147483647
[static] The largest representable 32-bit signed integer, which is 2,147,483,647.
int
  MIN_VALUE : int = -2147483648
[static] The smallest representable 32-bit signed integer, which is -2,147,483,648.
int
Constructor detail
int()constructor
public function int(num:Object)

Language version: ActionScript 3.0
Runtime version: 

Constructor; creates a new int object. You must use the int constructor when using int.toString() and int.valueOf(). You do not use a constructor when using the properties of an int object. The new int constructor is primarily used as a placeholder. An int object is not the same as the int() function that converts a parameter to a primitive value.

Parameters
num:Object — The numeric value of the int object being created or a value to be converted to a number. The default value is 0 if value is not provided.

See also


Example

Method detail
toExponential()method
AS3 function toExponential(fractionDigits:uint):String

Language version: ActionScript 3.0
Runtime version: AIR 1.0

Returns a string representation of the number in exponential notation. The string contains one digit before the decimal point and up to 20 digits after the decimal point, as specified by the fractionDigits parameter.

Parameters
fractionDigits:uint — An integer between 0 and 20, inclusive, that represents the desired number of decimal places.

Returns
String

Throws
RangeError — Throws an exception if the fractionDigits argument is outside the range 0 to 20.

Example
The following example shows how toExponential(2) returns a string in exponential notation.

var num:Number = 315003;
trace(num.toExponential(2)); // 3.15e+5

toFixed()method 
AS3 function toFixed(fractionDigits:uint):String

Language version: ActionScript 3.0
Runtime version: AIR 1.0

Returns a string representation of the number in fixed-point notation. Fixed-point notation means that the string will contain a specific number of digits after the decimal point, as specified in the fractionDigits parameter. The valid range for the fractionDigits parameter is from 0 to 20. Specifying a value outside this range throws an exception.

Parameters
fractionDigits:uint — An integer between 0 and 20, inclusive, that represents the desired number of decimal places.

Returns
String

Throws
RangeError — Throws an exception if the fractionDigits argument is outside the range 0 to 20.

Example
The following example shows how toFixed(3) returns a string that rounds to three decimal places.

var num:Number = 7.31343;
trace(num.toFixed(3)); // 7.313

The following example shows how toFixed(2) returns a string that adds trailing zeroes.

var num:Number = 4;
trace(num.toFixed(2)); // 4.00

toPrecision()method 
AS3 function toPrecision(precision:uint):String

Language version: ActionScript 3.0
Runtime version: AIR 1.0

Returns a string representation of the number either in exponential notation or in fixed-point notation. The string will contain the number of digits specified in the precision parameter.

Parameters
precision:uint — An integer between 1 and 21, inclusive, that represents the desired number of digits to represent in the resulting string.

Returns
String

Throws
RangeError — Throws an exception if the precision argument is outside the range 1 to 21.

Example
The following example shows how toPrecision(3) returns a string with only three digits. The string is in fixed-point notation because exponential notation is not required.

var num:Number = 31.570;
trace(num.toPrecision(3)); // 31.6

The following example shows how toPrecision(3) returns a string with only three digits. The string is in exponential notation because the resulting number does not contain enough digits for fixed-point notation.

var num:Number = 4000;
trace(num.toPrecision(3)); // 4.00e+3

toString()method 
AS3 function toString(radix:uint):String

Language version: ActionScript 3.0
Runtime version: 

Returns the string representation of an int object.

Parameters
radix:uint — Specifies the numeric base (from 2 to 36) to use for the number-to-string conversion. If you do not specify the radix parameter, the default value is 10.

Returns
String — A string.

Example

valueOf()method 
AS3 function valueOf():int

Language version: ActionScript 3.0
Runtime version: 

Returns the primitive value of the specified int object.

Returns
int — An int value.

Example

Constant detail
MAX_VALUEconstant
public static const MAX_VALUE:int = 2147483647

Language version: ActionScript 3.0
Runtime version: 

The largest representable 32-bit signed integer, which is 2,147,483,647.


Example

MIN_VALUEconstant 
public static const MIN_VALUE:int = -2147483648

Language version: ActionScript 3.0
Runtime version: 

The smallest representable 32-bit signed integer, which is -2,147,483,648.


Example

Examples
examples\IntExample
The following example uses the IntExample class to show how to work with and check the validity of int data types:
  1. Two int variables a and b are declared in the constructor.
  2. The two ints are added using the method addIntegers().
  3. A third int variable c is assigned the outcome of parseInteger(), which checks the validity of the string passed to it to ensure that it is an integer value in the acceptable range for int data types and returns an int equal to the integer value of the string if it is valid.
  4. The int variables a and c are added together using addIntegers().
package {
	import flash.display.Sprite;

	public class IntExample extends Sprite {	
		public function IntExample() {
			var a:int = 512;
			var b:int = -128;
			
			trace(addIntegers(a, b)); // 384

			var c:int = parseInteger("32");
			
			trace(addIntegers(a, c)); // 544
		}
			
		public function addIntegers(a:int, b:int):int {
			return a + b;
		}
		
		public function parseInteger(str:String):int {
			var num:Number = parseInt(str);
			if(!isNaN(num) && num <= int.MAX_VALUE && num >= int.MIN_VALUE) {
				return int(num);
			}
			
			return 0;
		}
		
	}
}