Actionscript Course

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.
Example:
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.
Example:
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.
Example:
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).

Methods

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

Constants

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:
Math.method(parameter)
  - 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:
Math.CONSTANT
  - Example:
trace(Math.PI);                        // 3.141592653589793

trace(Math.E);                        // 2.71828182845905

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Numbers and Math in ActionScript 3

Last accessed pages

  1. Display data from PHP Array, or MySQL in HTML table (26988)
  2. PHP Simple HTML DOM Parser (12469)
  3. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142527)
  4. Paragraphs, Line break, Horizontal rule (4310)
  5. Convert BBCode to HTML and HTML to BBCode with JavaScript (9417)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (530)
  2. The Mastery of Love (65)
  3. CSS cursor property - Custom Cursors (63)
  4. Read Excel file data in PHP - PhpExcelReader (59)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (44)