Number Class

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 18.  ActionScript Language Reference
Number Class Flash 5

wrapper class for primitive numeric data

Constructor

new Number(value)

Arguments

value

An expression to be resolved and, if necessary, converted to a numeric value, then wrapped in a Number object.

Class Properties

The following properties are accessed directly as properties of the Number class, using Number.propertyName. To access them you do not need to instantiate a new Number object (i.e., there is no need for the constructor function). Some of these properties, such as NaN, don't even require the Number.propertyName notation. You can simply use NaN as shorthand for Number.NaN (details follow in entries for each property).

MAX_VALUE

The largest representable positive number in ActionScript.

MIN_VALUE

The smallest representable positive number in ActionScript.

NaN

Special Not-a-Number value indicating invalid numeric data.

NEGATIVE_INFINITY

Any number more negative than -MAX_VALUE.

POSITIVE_INFINITY

Any number larger than MAX_VALUE.

Methods

toString( )

Convert a number to a string.

Description

The Number class has two purposes:

  • It gives us access to built-in properties that represent special numeric values MAX_VALUE, MIN_VALUE, NaN, NEGATIVE_INFINITY, and POSITIVE_INFINITY that can be used to check whether numeric data is valid or within the acceptable range.

  • It can be used to convert between different number systems, such as base-10 (decimal) and base-16 (hexadecimal). Refer to the Number.toString( ) method for details.

Usage

There is no need to create a Number object if you simply want to access the numeric properties it defines. In fact, where applicable, it is easier to use global property equivalents (NaN, Infinity, and -Infinity). Frankly, it is rare that you'll need the Number class properties at all.

On the other hand, the Number class's toString( ) method is used with a Number object. However, the interpreter takes care of creating a Number instance for us whenever we invoke a method on a primitive numeric value. For example, here we use toString( ) to convert a decimal number to its hexadecimal equivalent:

x = 102; hex = x.toString(16);  // A Number instance is automatically and (temporarily)                        // created to "wrap" x for the sake of this operation.

As this is a fairly rare task, you probably won't be using instances of the Number class much.

See Also

The Math object; "Section 4.1



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net