ActionScript® 3.0 Reference for the Adobe® Flash® Platform
Home  |  Show Packages and Classes List |  Packages  |  Classes  |  What's New  |  Index  |  Appendixes
Top Level 

Number  - AS3

PackageTop Level
Classpublic final class Number
InheritanceNumber Inheritance Object

Language Version: ActionScript 3.0
Runtime Versions: AIR 1.0, Flash Player 9, Flash Lite 4

A data type representing an IEEE-754 double-precision floating-point number. You can manipulate primitive numeric values by using the methods and properties associated with the Number class. This class is identical to the JavaScript Number class.

The properties of the Number class are static, which means you do not need an object to use them, so you do not need to use the constructor.

The Number data type adheres to the double-precision IEEE-754 standard.

The Number data type is useful when you need to use floating-point values. Flash runtimes handle int and uint data types more efficiently than Number, but Number is useful in situations where the range of values required exceeds the valid range of the int and uint data types. The Number class can be used to represent integer values well beyond the valid range of the int and uint data types. The Number data type can use up to 53 bits to represent integer values, compared to the 32 bits available to int and uint. The default value of a variable typed as Number is NaN (Not a Number).

View the examples

Related API Elements



Public Properties
 PropertyDefined By
 Inheritedconstructor : Object
A reference to the class object or constructor function for a given object instance.
Object
Public Methods
 MethodDefined By
  
Creates a Number object with the specified value.
Number
 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.
Number
  
toFixed(fractionDigits:uint):String
Returns a string representation of the number in fixed-point notation.
Number
 Inherited
Returns the string representation of this object, formatted according to locale-specific conventions.
Object
  
Returns a string representation of the number either in exponential notation or in fixed-point notation.
Number
  
toString(radix:Number = 10):String
Returns the string representation of the specified Number object (myNumber).
Number
  
Returns the primitive value type of the specified Number object.
Number
Public Constants
 ConstantDefined By
  MAX_VALUE : Number
[static] The largest representable number (double-precision IEEE-754).
Number
  MIN_VALUE : Number
[static] The smallest (closest to zero, not most negative) representable IEEE-754 number that does not compare equal to zero.
Number
  NaN : Number
[static] The IEEE-754 value representing Not a Number (NaN).
Number
  NEGATIVE_INFINITY : Number
[static] Specifies the IEEE-754 value representing negative infinity.
Number
  POSITIVE_INFINITY : Number
[static] Specifies the IEEE-754 value representing positive infinity.
Number
Constructor Detail

Number

()Constructor
public function Number(num:Object)

Language Version: ActionScript 3.0
Runtime Versions: AIR 1.0, Flash Player 9, Flash Lite 4

Creates a Number object with the specified value. This constructor has the same effect as the Number() public native function that converts an object of a different type to a primitive numeric value.

Parameters
num:Object — The numeric value of the Number instance being created or a value to be converted to a Number. The default value is 0 if num is not specified. Using the constructor without specifying a num parameter is not the same as declaring a variable of type Number with no value assigned (such as var myNumber:Number), which defaults to NaN. A number with no value assigned is undefined and the equivalent of new Number(undefined).

Related API Elements

Method Detail

toExponential

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

Language Version: ActionScript 3.0
Runtime Versions: Flash Player 9, AIR 1.0, Flash Lite 4

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  ( How to use this 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 Versions: Flash Player 9, AIR 1.0, Flash Lite 4

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  ( How to use this 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 Versions: Flash Player 9, AIR 1.0, Flash Lite 4

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  ( How to use this 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:Number = 10):String

Language Version: ActionScript 3.0
Runtime Versions: AIR 1.0, Flash Player 9, Flash Lite 4

Returns the string representation of the specified Number object (myNumber). If the value of the Number object is a decimal number without a leading zero (such as .4), Number.toString() adds a leading zero (0.4).

Parameters

radix:Number (default = 10) — 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 — The numeric representation of the Number object as a string.

valueOf

()method 
AS3 function valueOf():Number

Language Version: ActionScript 3.0
Runtime Versions: AIR 1.0, Flash Player 9, Flash Lite 4

Returns the primitive value type of the specified Number object.

Returns
Number — The primitive type value of the Number object.
Constant Detail

MAX_VALUE

Constant
public static const MAX_VALUE:Number

Language Version: ActionScript 3.0
Runtime Versions: AIR 1.0, Flash Player 9, Flash Lite 4

The largest representable number (double-precision IEEE-754). This number is approximately 1.79e+308.

MIN_VALUE

Constant 
public static const MIN_VALUE:Number

Language Version: ActionScript 3.0
Runtime Versions: AIR 1.0, Flash Player 9, Flash Lite 4

The smallest (closest to zero, not most negative) representable IEEE-754 number that does not compare equal to zero. On most platforms this number is approximately 5e-324, but it can be larger on systems that do not support denormalized numbers. For example, on iOS this constant's value is 2.225e-308.

Note: The absolute smallest representable number overall is -Number.MAX_VALUE.

NaN

Constant 
public static const NaN:Number

Language Version: ActionScript 3.0
Runtime Versions: AIR 1.0, Flash Player 9, Flash Lite 4

The IEEE-754 value representing Not a Number (NaN).

Related API Elements

NEGATIVE_INFINITY

Constant 
public static const NEGATIVE_INFINITY:Number

Language Version: ActionScript 3.0
Runtime Versions: AIR 1.0, Flash Player 9, Flash Lite 4

Specifies the IEEE-754 value representing negative infinity. The value of this property is the same as that of the constant -Infinity.

Negative infinity is a special numeric value that is returned when a mathematical operation or function returns a negative value larger than can be represented.

POSITIVE_INFINITY

Constant 
public static const POSITIVE_INFINITY:Number

Language Version: ActionScript 3.0
Runtime Versions: AIR 1.0, Flash Player 9, Flash Lite 4

Specifies the IEEE-754 value representing positive infinity. The value of this property is the same as that of the constant Infinity.

Positive infinity is a special numeric value that is returned when a mathematical operation or function returns a value larger than can be represented.

NumberExample.as

The following example shows how a number with six digits after the decimal place is truncated (with rounding) to a number with two digits after the decimal point.

package {
    import flash.display.Sprite;

    public class NumberExample extends Sprite {
        public function NumberExample() {
            var num:Number = new Number(10.456345);
            var str:String = num.toFixed(2);
            trace(num); // 10.456345
            trace(str); // 10.46
        }
    }
}