JavaScript Number Class



Navigation Aids -- This Page        Navigation Aids -- This Topic        Navigation Aids -- This Site








Quick Reference for the Number Class



The Number Class Reference Table
Native class
Inherits From Object
Constants Properties Methods Constructors
Syntax Arguments Returns
 
MAX_VALUE
MIN_VALUE
NaN
NEGATIVE_INFINITY
POSITIVE_INFINITY
None toFixed( )
toExponential( )
toPrecision( )
toLocaleString( )
toString( )
valueOf( )
new Number( ) value A new number.




Number Class Description

The Number class is a wrapper class for the primitive number data type. Because the Number object will behave like any other object, the following behavior and characteristics apply:





Number Class Constants

The Number class has five constants. These constants are properties of the Number class constructor. In OOD terms we would call them class properties. As a review, to use a class property, we must use the class name (Number) along with the property name joined together by the access operator (dot operator).

if (myNumber == Number.NaN)
The Pre-defined Constants of the Number Class
Constant Value Description
Footnote:  The special values of NaN, Infinity and -Infinity are properties of the Global class.
Number.MAX_VALUE 1.7976931348623157 X 10308 A constant that holds the largest possible numeric value that can be represented.
Number.MIN_VALUE 5 X 10-324 A constant that holds the smallest possible numeric value that can be represented.
Number.NaN The special value NaN. Not-a-Number.
Number.NEGATIVE_INFINITY The special value -Infinity. A value that is too small to be represented; eg. MIN_VALUE - 1 would result in -Infinity.
Number.POSITIVE_INFINITY The special value Infinity. A value that is too large to be represented; eg. MAX_VALUE + 1 would result in Infinity.




Number Class Instance Properties

There are no explicit properties for the Number class.

The Number class does inherit the following properties from the base Object class.





Number Class Instance Methods

The Number class does inherit some methods from the base Object class.

The explicit methods of the Number class are shown below. Some of these methods (valueOf, toLocaleString and toString) override the corresponding methods of the base Object class.



The Methods for the Number Class
Method Syntax / Arguments Returns Description / Example
 
toExponential( ) Syntax:
obj.toExponential(places)

Arguments: places
places: the number of decimal places
Returns a string representing the Number object. The toExponential( ) method behaves as follows:
  • returns a string version of the number wrapped within the Number object
  • the returned value is in exponential notation
  • the returned value is always expressed with one significant digit to the left of the decimal point
  • the optional argument specifies the length of the significand
  • the significand is rounded or padded with zeros to comply with the specified number of decimal places given with the argument
  • the argument may be a value from 0 to 20
  • when omitted, the significand is as large as necessary
  • the method will throw a TypeError exception when the method is invoked on an object that is not a Number
  • the method will throw a RangeError exception when the argument specified is not within the range of 0 to 20

Example:
//instantiate a number object named no var no = new Number(1234.56789); no.toExponential(1));// returns 1.2e+3 no.toExponential(2));// returns 1.23e+3 no.toExponential(0));// returns 1e+3 //rounding and padding follow no.toExponential(5));// returns 1.23457e+3 no.toExponential(12));// returns 1.234567890000e+3
toFixed( ) Syntax:
obj.toFixed(places)

Arguments: places
places: the number of decimal places
Returns a string representing the Number object. The toFixed( ) method behaves as follows:
  • returns a string version of the number wrapped within the Number object
  • this method is similar to toExponential, however, the returned value is not in exponential notation
  • the optional argument specifies the length of the significand
  • the significand is rounded or padded with zeros to comply with the specified number of decimal places given with the argument
  • the argument may be a value from 0 to 20
  • when omitted, the returned value represents an integer
  • the method will throw a TypeError exception when the method is invoked on an object that is not a Number
  • the method will throw a RangeError exception when the argument specified is not within the range of 0 to 20

Example:
//instantiate a number object named no var no = new Number(1234.56789); no.toFixed());// returns 1235 no.toFixed(0));// returns 1235 no.toFixed(1));// returns 1234.6 no.toFixed(2));// returns 1234.57 no.toFixed(5));// returns 1234.56789 no.toFixed(12));// returns 1234.567890000000
toPrecision( ) Syntax:
obj.toPrecision(precision)

Arguments: precision
precision: the number of significant digits
Returns a string representing the Number object. The toPrecision( ) method behaves as follows:
  • returns a string version of the number wrapped within the Number object
  • the argument may be a value between 1 and 21
  • when the argument is omitted, the toString method is substituted in the place of toPrecision
  • given the size of the integer portion of the number and the argument precision value, the returned value may be represented by two separate notations
    • fixed-point notation: when the argument is large enough to include all of the digits of the integer part of the number
    • exponential notation: when the argument does not allow for all of the digits of the integer portion of the number
  • when exponential notation applys:
    • the returned value is expressed with one significant digit to the left of the decimal point
    • the digits to the right of the decimal are argument - 1 in length
  • rounding and zero padding will apply as necessary
  • the method will throw a TypeError exception when the method is invoked on an object that is not a Number
  • the method will throw a RangeError exception when the argument specified is not within the range of 1 to 21

Example:
//instantiate a number object named no var no = new Number(1234.56789); no.toPrecision(1));// returns 1e+3 no.toPrecision(2));// returns 1.2e+3 no.toPrecision(3));// returns 1.23e+3 no.toPrecision(4));// returns 1235 no.toPrecision(5));// returns 1234.6 no.toPrecision(0));// script error no.toPrecision(12));// returns 1234.56789000
toLocaleString( )

Syntax:
obj.toLocaleString( )

Arguments: none
Returns a string. The toLocaleString( ) method behaves as follows:
  • returns a string version of the object
  • the returned string may represent implementation dependent formatting (punctuation characters used for numbers)
  • see the toString( ) method for further details
  • the method will throw a TypeError exception when the method is invoked on an object that is not a Number
toString( ) Syntax:
obj.toString(radix)

Arguments: radix
radix: an optional argument that specifies the radix
Returns a string representing the Number. The toString( ) method behaves as follows:
  • returns a string version of the number wrapped within the Number object
  • the radix argument is optional
  • when the radix argument is omitted, the number is represented in a base-10 string
  • the method will throw a TypeError exception when the method is invoked on an object that is not a Number.
The toString( ) method behaves as follows with regards to the radix argument:
  • the valid base numbers for the radix argument are 2 through 36
  • a radix less than 2 or greater than 36 results with a syntax error
  • in the table below, assume number in object x = 11
    Base System Range of
    Numbers
    Expression Result
    2 Binary 0 and 1 x.toString(2) 1011
    8 Octal 0 thru 7 x.toString(8) 13
    10 Decimal 0 thru 9 x.toString(10) 11
    16 Hex 0 thru 9
    a thru f
    x.toString(16) b

Example:
var numobj = new Number(11); // 11 is argument to constructor alert(numobj.toString()); // returns string of 11
valueOf( ) Syntax:
obj.valueOf( )

Arguments: None
Returns a primitive number value The valueOf( ) method behaves as follows:
  • valueOf( ) returns the primitive number value contained in the Number object
  • the method will throw a TypeError exception when the method is invoked on an object that is not a Boolean.
Example:
// Number object with number as argument var numobj = new Number(98765); alert(numobj.valueOf());//return 98765




Number Class Constructor





Number Class Constructor Example



Top            

Rx4AJAX        About Us | Topic Index | Contact Us | Privacy Policy | 2008 This Site Built By PPThompson