Section 24.128. Number: support for numbers


24.128. Number: support for numbers

ECMAScript v1: Object Number

24.128.1. Constructor

 new Number(value) Number(value) 

24.128.1.1. Arguments

value

The numeric value of the Number object being created or a value to be converted to a number.

24.128.1.2. Returns

When Number( ) is used with the new operator as a constructor, it returns a newly constructed Number object. When Number( ) is invoked as a function without the new operator, it converts its argument to a primitive numeric value and returns that value (or NaN if the conversion failed).

24.128.2. Constants


Number.MAX_VALUE

The largest representable number.


Number.MIN_VALUE

The smallest representable number.


Number.NaN

Not-a-number value.


Number.NEGATIVE_INFINITY

Negative infinite value; returned on overflow.


Number.POSITIVE_INFINITY

Infinite value; returned on overflow.

24.128.3. Methods


toString( )

Converts a number to a string using a specified radix (base).


toLocaleString( )

Converts a number to a string using local number-formatting conventions.


toFixed( )

Converts a number to a string that contains a specified number of digits after the decimal place.


toExponential( )

Converts a number to a string using exponential notation with the specified number of digits after the decimal place.


toPrecision( )

Converts a number to a string using the specified number of significant digits. Uses exponential or fixed-point notation depending on the size of the number and the number of significant digits specified.


valueOf( )

Returns the primitive numeric value of a Number object.

24.128.4. Description

Numbers are a basic, primitive datatype in JavaScript. JavaScript also supports the Number object, which is a wrapper object around a primitive numeric value. JavaScript automatically converts between the primitive and object forms as necessary. You can explicitly create a Number object with the Number( ) constructor, although there is rarely any need to do so.

The Number( ) constructor can also be used without the new operator, as a conversion function. When invoked in this way, it attempts to convert its argument to a number and returns the primitive numeric value (or NaN) that results from the conversion.

The Number( ) constructor is also used as a placeholder for five useful numeric constants: the largest and smallest representable numbers, positive and negative infinity, and the special NaN value. Note that these values are properties of the Number( ) constructor function itself, not of individual number objects. For example, you can use the MAX_VALUE property as follows:

 var biggest = Number.MAX_VALUE 

but not like this:

 var n = new Number(2); var biggest = n.MAX_VALUE 

By contrast, the toString( ) and other methods of the Number object are methods of each Number object, not of the Number( ) constructor function. As noted earlier, JavaScript automatically converts from primitive numeric values to Number objects whenever necessary. This means that you can use the Number methods with primitive numeric values as well as with Number objects.

 var value = 1234; var binary_value = n.toString(2); 

24.128.5. See Also

Infinity, Math, NaN




JavaScript. The Definitive Guide
JavaScript: The Definitive Guide
ISBN: 0596101996
EAN: 2147483647
Year: 2004
Pages: 767

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net