
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Numeric Literals
|
81
}
}
// Round a number to two decimal places
trace(roundNum(1.12645, 2)); // Displays: 1.13
Special Values of the Number Datatype
Integer and floating-point literals account for nearly all the legal values of the num-
ber datatype, but there are special keyword values that represent the following
numeric concepts: Not-a-Number, Minimum Allowed Value, Maximum Allowed
Value, Infinity, and Negative Infinity.
Each of the special values can be assigned to variables and properties or used in lit-
eral expressions, just like any other numeric literal. More often than not, though, the
special numeric values are returned by the interpreter as the result of some expres-
sion evaluation.
Not-a-Number: NaN
Occasionally, a mathematical computation or an attempted datatype conversion
results in a value that simply is not a number. For example, 0/0 is an impossible cal-
culation, and the following expression can’t be converted to a finite number:
23 – "go ahead and try!"
In order to accommodate data that is of the number type but is not a calculable num-
ber, ActionScript provides the
NaN keyword value. Though NaN doesn’t represent a
calculable number, it is still a legal value of the number type, as demonstrated by the
following code:
x = 0/0;
trace(x); // Displays: NaN
trace(typeof x); ...