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

Click on the HTML tag which creates an horizontal line in web page.
<br /> <em> <hr />
Some content ...
<hr />
Content under line ...
Which CSS property defines the text color?
font-style font-variant color
h2 {
  color: #cbdafb;
Click on the function which searches if a character, or text exists in a string.
indexOf() toString() split()
var str = "Web courses -";
if(str.indexOf("http://") == -1) alert("http:// isn`t in string");
else alert("http:// is in string");
Which function splits a string into an array of strings based on a separator?
array_merge() explode() implode()
$str = "apple,banana,melon,pear";
$arr = explode(",", $str);
var_export($arr);      // array (0=>"apple", 1=>"banana", 2=>"melon", 3=>"pear")
Numbers and Math in ActionScript 3

Last accessed pages

  1. jQuery ajax() method (13427)
  2. Selection Tools (4056)
  3. Texarea with buttons to format text, colors and smiles (2773)
  4. Using file_put_contents, file_get_contents, readfile and file() (5435)
  5. Bubbles3 (47680)

Popular pages this month

  1. Qwop (3462)
  2. Drag Racer V3 (1707)
  3. Flash Games - Free online Games (810)
  4. Bubbles3 (810)
  5. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (758)