Number object

Numbers are any number which is not included within quotes (if you add a number between quotes, will be interpreted as a String).
There are several types of numbers:
  - Natural numbers - positive whole numbers (8, 15, 89, 1234).
  - Integer numbers - whole numbers that might be either positive or negative (-78 , -3 , 0 , 5 , 78).
  - Floating-point numbers (-6.4 , 0.8 , 12.5).
  - Hexadecimal numbers (0x7d8 is number 2008).
  - Exponential numbers - used for large numerical values ( 7.8e+8 = 780 000 000, is 7.8x108 ).

Everything in ActionScript 3.0 is an object, including numbers. In ActionScript there are special classes for working with numbers.
You can create a variable that contains numerical values by using any of the following syntaxes:
                var var_name:NumberType = number;
                var var_name:NumberType = new NumberType(number);
                var var_name:NumberType = NumberType(number);
- NumberType represents the ActionScript class for the type of numbers that are stored in the "var_name" variable, can be 3 types: - Usually the first syntax is used.
var num1:int = 78;
var num2:Number = new Number(68.8);
var num3:uint = uint(23);

NaN - Not a number

If you declare a Number type variable, without assign a value, the variable will have the defaul value, NaN (which represents "not a number").
Mathematical operations with non-real or undefined results also yield NaN, such as the square root of -1 or zero divided by zero.
var num1:Number;
var num2:Number = 0/0;

trace(num1);        // NaN
trace(num2);        // NaN

To check if a variable has the NaN value, use the isNaN(num) function. This function returns true if the value of "num" parameter is NaN, otherwise, returns false.
var num:Number;
trace(num);            // NaN
trace(isNaN(num));     // true

// check if the value of "num" is NaN, and assign a numeric value
if(isNaN(num)) {
  num = 78;
trace(num);            // 78

Other things about numbers in ActionScript

- var num:Number = .4;   and   var nr:Number = 0.45;   represent the same number (0.45).
- The default value for int and uint types is 0.
- To define a color in ActionScript, it uses the expression: 0xFFRRGGBB (F - alpha (transparency), R - red, G - green, B - blue), this is a 32-bits value which can be stored in uint variable (e.g., var getcolor:uint = 0xafe8edfe; ).

Methods and Constants of the Number object

The Number object has no specific properties, but has a few methods and constants.
To perform arithmetic operations with numbers, use the arithmetic and relational operators (see the lesson Constants and Operators).


toExponential(dg) - returns a string representation of the number in exponential notation. The "dg" parameter represents the number of digits before the decimal point.
var nr:Number = 123456789;
trace(nr.toExponential(2));             // 1.23e+8

toFixed(dg) - returns a string representation of the number in fixed-point notation. The "dg" parameter represents the number of digits before the decimal point.
var nr:Number = 12.3456789;
trace(nr.toFixed(3));             // 12.346

toPrecision(dg) - 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 "dg" parameter.
var nr1:Number = 12.3456789;
trace(nr1.toPrecision(4));             // 12.35

var nr2:Number = 123400000;
trace(nr2.toPrecision(4));             // 1.234e+8

toString() - converts the object into string.
var nr:int = 7;
var str:String = '8';
trace(nr.toString()+ str);             // 78     (or String(nr)+ str )

Number('str') - converts the 'str' string into number.
var nr:int = 7;
var str:String = '8';
trace(nr+ Number(str));             // 15


MAX_VALUE - the largest posible number
trace(int.MAX_VALUE);              // 2147483647
trace(uint.MAX_VALUE);             // 4294967295
trace(Number.MAX_VALUE);           // 1.79769313486231e+308

MIN_VALUE - The smallest representable number
trace(int.MIN_VALUE);              // -2147483648
trace(uint.MIN_VALUE);             // 0
trace(Number.MIN_VALUE);           // 4.9406564584124654e-324

NEGATIVE_INFINITY - represents the negative infinity. It can be used to check the negative infinite conditions.
var nr:Number = -7/0;
trace();             // -Infinity
trace(nr == Number.NEGATIVE_INFINITY);             // true

POSITIVE_INFINITY - represents the positive infinity. It can be used to check the positive infinite conditions.
var nr:Number = 8/0;
trace();             // Infinity
trace(nr == Number.POSITIVE_INFINITY);             // true

Math class

The Math class contains methods and constants to perform complex mathematical operations.
To apply the methods of the Math class, use the following syntax:
  - Example:
var nr:Number = Math.pow(3, 4);        // 3 raised to power 4
trace(nr);                             // 81

var nr2:Number = Math.random();        // get a random value between 0 and 1
trace(nr2);                            // 0.32177373580634594

Math mehods for Arithmetic operations

Math mehods for Trigonometric calculations

- All trig functions operate on radians, an angular unit in which 2π radians measure a full revolution.

To get the radians value when you know the degrees, use the following formula:
radians = degrees * Math.PI/180;
  - Example:
var degrees:Number = 45;
var radians:Number = degrees * Math.PI/180;
trace(radians);               // 0.7853981633974483

To get the degrees when you know the radians value, use the following formula:
degrees = radians * 180/Math.PI;
  - Example:
var radians:Number = 0.7854;
var degrees:Number = radians * 180/Math.PI;
trace(degrees);               // 45

Math Constants

Constants contain a fixed value. To apply a constant, use the following syntax:
  - Example:
trace(Math.PI);                        // 3.141592653589793

trace(Math.E);                        // 2.71828182845905

Daily Test with Code Example

Which tag defines the clickable areas inside the image map?
<map> <img> <area>
<img src="image.jpg" usemap="#map1">
<map name="map1">
  <area shape="rect" coords="9, 120, 56, 149" href="#">
  <area shape="rect" coords="100, 200, 156, 249" href="#">
Which CSS property defines what is done if the content in a box is too big for its defined space?
display overflow position
#id {
  overflow: auto;
Click on the event which is triggered when the mouse is positioned over an object.
onclick onmouseover onmouseout
document.getElementById("id").onmouseover = function(){
  document.write("Have Good Life");
Indicate the PHP variable that contains data added in URL address after the "?" character.
if(isset($_GET["id"])) {
  echo $_GET["id"];
Numbers and Math in ActionScript 3

Last accessed pages

  1. The Mastery of Love (1688)
  2. Adventure Games (4770)
  3. Qwop (39689)
  4. innerHTML in PHP (5012)
  5. Tic Tac Toe Note Paper (211)

Popular pages this month

  1. Qwop (6864)
  2. Drag Racer V3 (4694)
  3. Bubbles3 (1916)
  4. List with All the Games (1605)
  5. Butterfly Kyodai (1507)