Name

int( ) Global Function — truncate the decimal portion of a number

Availability

Flash 4; deprecated in Flash 5 in favor of analogous Math methods

Synopsis

int(number)

Arguments

number

A number or an expression that yields a number, typically a number with a fractional (decimal) portion.

Returns

The integer portion of number.

Description

The int( ) function was used in Flash 4 as a brute-force means of extracting the integer portion of a number. It effectively rounds positive numbers down and rounds negative numbers up. The int( ) function works only for numbers in the range -2147483648 (-231) to 2147483647 (231-1); it produces undefined results for numbers outside this range. If number is a string composed of only numbers, int( ) converts the string to a number before operating on it. If number is the Boolean value true, int( ) returns the value 1. For all other non-numeric data (including undefined and null), int( ) returns the value 0.

Usage

The int( ) function has been deprecated in favor of the more precise and standard Math.floor( ), Math.ceil( ), and Math.round( ) methods. Use parseInt( ) or Number( ) to convert non-numeric data to an integer or number.

Example

int(4.5)      // Yields 4
int(-4.5)     // Yields -4
int(3.999)    // Yields 3

The int( ) function is useful to check if a number is a whole number by comparing the original number to the result of the int( ) function:

if (int(x) != x) {
  trace ("Please enter a whole number for your age in years");
}

See Also

Math.ceil( ), Math.floor( ),

Get ActionScript: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.