本文档基于 CC协议(Creative Commons Share-Alike License)发布,是developer.mozilla.com, www.mozref.comwww.aptana.com共同劳动的成果。

Number : Object

Lets you work with numeric values. The Number object is an object wrapper for primitive numeric values.

Browser/User Agent Support

IEMozillaNetscapeOperaSafari
3.0+1.0+3.0+7.0+1.0+

Constructors

ConstructorIEMozillaNetscapeOperaSafari
Creates a new instance of a Number.
3.0+1.0+3.0+7.0+1.0+
 

Properties

PropertyIEMozillaNetscapeOperaSafari
The maximum numeric value representable in JavaScript.
4.0+1.0+3.0+7.0+1.0+
 
The smallest positive numeric value representable in JavaScript.
4.0+1.0+3.0+7.0+1.0+
 
A value representing Not-A-Number.
4.0+1.0+3.0+7.0+1.0+
 
Special value representing negative infinity; returned on overflow.
4.0+1.0+3.0+7.0+1.0+
 
Special value representing infinity; returned on overflow.
4.0+1.0+3.0+7.0+1.0+
 
Specifies the function that creates the Number prototype.
4.0+1.0+3.0+7.0+1.0+
 
Reference to the Number prototype object.
4.0+1.0+3.0+7.0+1.0+
 

Methods

MethodIEMozillaNetscapeOperaSafari
Returns a string representing the number in exponential notation.
5.5+1.0+6.0+7.0+1.0+
 
Returns a string representing the number in fixed-point notation.
5.5+1.0+6.0+7.0+1.0+
 
Returns a string representing the number that follows local formatting conventions.
5.5+1.0+6.0+7.0+1.0+
 
Returns a string representing the number to a specified precision in fixed-point notation.
5.5+1.0+6.0+7.0+1.0+
 
Returns an object literal representing the specified Number object; you can use this value to create a new object.
4.0+1.0+3.0+nono
 
Returns a string representing the specified object.
4.0+1.0+3.0+7.0+1.0+
 
Returns the primitive value of the specified object.
4.0+1.0+3.0+7.0+1.0+
 

Using the Number object to assign values to numeric variables

The following example uses the Number object's properties to assign values to several numeric variables:

biggestNum = Number.MAX_VALUE;
smallestNum = Number.MIN_VALUE;
infiniteNum = Number.POSITIVE_INFINITY;
negInfiniteNum = Number.NEGATIVE_INFINITY;
notANum = Number.NaN;

Using Number object to modify all Number objects

The following example creates a Number object, myNum, then adds a description property to all Number objects. Then a value is assigned to the myNum object's description property.

myNum = new Number(65);
Number.prototype.description = null;
myNum.description = "wind speed";

Remarks

The primary uses for the Number object are:

The properties of Number are properties of the class itself, not of individual Number objects.

JavaScript 1.2: Number(x) now produces NaN rather than an error if x is a string that does not contain a well-formed numeric literal. For example, the following prints NaN:

x=Number("three");
document.write(x + "<BR>");

You can convert any object to a number using the top-level Number function.

References

Global.Infinity | Math | Global.NaN

Availability

JavaScript 1.1 | JScript 2.0 | ECMAScript v1

Constructor Detail

Number Number(Number value)

Creates a new instance of a Number.

NumbervalueThe numeric value of the object being created.

Property Detail

static Number MAX_VALUE - read only

The maximum numeric value representable in JavaScript.

Using MAX_VALUE

The following code multiplies two numeric values. If the result is less than or equal to MAX_VALUE, the func1 function is called; otherwise, the func2 function is called.

if (num1 * num2 <= Number.MAX_VALUE)
   func1();
else
   func2();
Remarks

The MAX_VALUE property has a value of approximately 1.79E+308. Values larger than MAX_VALUE are represented as "Infinity".

Because MAX_VALUE is a static property of Number, you always use it as Number.MAX_VALUE, rather than as a property of a Number object you created.

Availability

JavaScript 1.1 | JScript 2.0, ECMAScript v1

static Number MIN_VALUE - read only

The smallest positive numeric value representable in JavaScript.

Using MIN_VALUE

The following code divides two numeric values. If the result is greater than or equal to MIN_VALUE, the func1 function is called; otherwise, the func2 function is called.

if (num1 / num2 >= Number.MIN_VALUE)
   func1()
else
   func2()
Remarks

The MIN_VALUE property is the number closest to 0, not the most negative number, that JavaScript can represent.

MIN_VALUE has a value of approximately 5e-324. Values smaller than MIN_VALUE ("underflow values") are converted to 0.

Because MIN_VALUE is a static property of Number, you always use it as Number.MIN_VALUE, rather than as a property of a Number object you created.

Availability

JavaScript 1.1 | JScript 2.0, ECMAScript v1

static Number NaN - read only

A value representing Not-A-Number.

Using NaN

In the following example, if month has a value greater than 12, it is assigned NaN, and a message is displayed indicating valid values.

var month = 13
if (month < 1 || month > 12) {
   month = Number.NaN
   alert("Month must be between 1 and 12.")
}
Remarks

The value of Number.NaN is Not-A-Number, same as the value of global object's NaN property.

NaN is always unequal to any other number, including NaN itself; you cannot check for the not-a-number value by comparing to Number.NaN. Use the isNaN function instead.

Several JavaScript methods (such as the Number constructor, parseFloat, and parseInt) return NaN if the value specified in the parameter can not be parsed as a number.

You might use the NaN property to indicate an error condition for your function that returns a number in case of success.

JavaScript prints the value Number.NaN as NaN.

See Also

isNaN | Number.NaN | parseFloat | parseInt

Availability

JavaScript 1.1 | JScript 2.0, ECMAScript v1

static Number NEGATIVE_INFINITY - read only

Special value representing negative infinity; returned on overflow.

See Also

Global.Infinity | isFinite

Availability

JavaScript 1.1 | JScript 2.0, ECMAScript v1

static Number POSITIVE_INFINITY - read only

Special value representing infinity; returned on overflow.

See Also

Global.Infinity | isFinite

Availability

JavaScript 1.1 | JScript 2.0, ECMAScript v1

 Object constructor -  only

Specifies the function that creates the Number prototype.

See Also

Object.constructor

Availability

JavaScript 1.1

 Object prototype -  only

Reference to the Number prototype object.

Availability

JavaScript 1.1

Method Detail

toExponential([Number digits]) : String

Returns a string representing the number in exponential notation.

NumberdigitsNumber of decimal places to appear after the decimal point. (optional)

Throws
Throws RangeError if digits is too small or too large of a number.
Throws TypeError if the method is invoked on an object that is not a Number.
See Also

Number.toFixed | Number.toLocaleString | Number.toPrecision | Number.toString

Availability

JavaScript 1.5 | JScript 5.5, ECMAScript v3

toFixed([Number digits]) : String

Returns a string representing the number in fixed-point notation.

NumberdigitsNumber of decimal places to appear after the decimal point. (optional)

Throws
Throws RangeError if digits is too small or too large of a number.
Throws TypeError if the method is invoked on an object that is not a Number.
See Also

Number.toExponential | Number.toLocaleString | Number.toPrecision | Number.toString

Availability

JavaScript 1.5 | JScript 5.5, ECMAScript v3

toLocaleString() : String

Returns a string representing the number that follows local formatting conventions.

Throws
Throws TypeError if the method is invoked on an object that is not a Number.
See Also

Number.toExponential | Number.toFixed | Number.toPrecision | Number.toString

Availability

JavaScript 1.5 | JScript 5.5, ECMAScript v3

toPrecision([String precision]) : String

Returns a string representing the number to a specified precision in fixed-point notation.

StringprecisionThe number of significant digits to use in the returned string. (optional)

Throws
Throws RangeError if digits is too small or too large of a number.
Throws TypeError if the method is invoked on an object that is not a Number.
See Also

Number.toExponential | Number.toFixed | Number.toLocaleString | Number.toString

Availability

JavaScript 1.5 | JScript 5.5, ECMAScript v3

toSource() :  Object

Returns an object literal representing the specified Number object; you can use this value to create a new object.

Availability

toString([Number radix]) : String

Returns a string representing the specified object.

NumberradixThe radix (or base) between 2 and 36 to use to represent the number. (optional)

Throws
Throws TypeError if the method is invoked on an object that is not a Number.
See Also

Number.toExponential | Number.toFixed | Number.toLocaleString | Number.toPrecision

Availability

JavaScript 1.1 | JScript 2.0, ECMAScript v1

valueOf() : Number

Returns the primitive value of the specified object.

Throws
Throws TypeError if the method is invoked on an object that is not a Number.
See Also

Object.valueOf

Availability

JavaScript 1.1 | JScript 2.0, ECMAScript v1