Number object (Object/core)

An object of the class "Number".

Availability:

ECMAScript edition - 2
JavaScript - 1.1
JScript - 1.0
Internet Explorer - 3.02
Netscape - 3.0
Netscape Enterprise Server version - 2.0
Opera - 3.0
JavaScript syntax:-myNumber = new Number()
-myNumber = Number
Object properties:constructor, prototype
Class constants:MAX_VALUE, MIN_VALUE, NaN, NEGATIVE_INFINITY, POSITIVE_INFINITY
Object methods:toExponential(), toFixed(), toLocaleString(), toPrecision(), toSource(), toString(), valueOf()

An instance of the class Number is created by using the new operator on the Number() constructor. The new object adopts the behavior of the built-in prototype object through the prototype-inheritance mechanisms.

All properties and methods of the prototype are available as if they were part of the instance.

A number object is a member of the type Object and is an instance of the built-in Number object.

Number objects are created by cloning the built-in Number object. This is done by calling the Number constructor with the new operator being applied to an existing Number object. Thus:

   myNumber = new Number(1000);

A Number object can be coerced to a number value and can be used anywhere where a number value would be expected.

Programmers familiar with object-oriented techniques may prefer to use the Number object while procedural language programmers may implement the same functionality with a number value instead.

This is an example of the flexibility of JavaScript in its ability to accommodate a variety of users from different backgrounds.

The prototype for the Number prototype object is the Object prototype object.

You might want to add useful methods to the Number.prototype to output numbers in unusual formats. For example you could implement a roman numeral conversion method. Adding that to the prototype would let you output year numbers in classical formats.

Warnings:

See also:Constant, Limits, Native object, Number.Class, Number.prototype, Object object

PropertyJavaScriptJScriptNIEOperaNESECMADOMCSSHTMLNotes
constructor1.1 1.11.0 1.03.0 3.03.02 3.02 n/a n/a2 2 n/a n/a n/a-
prototype1.1 1.11.0 1.03.0 3.03.02 3.023.0 3.02.0 2.02 2 n/a n/a n/aDontDelete, DontEnum.

MethodJavaScriptJScriptNIEOperaNESECMADOMCSSHTMLNotes
toExponential()1.5 1.55.5 5.56.0 6.05.5 5.5 n/a n/a3 3 n/a n/a n/a-
toFixed()1.5 1.55.5 5.56.0 6.05.5 5.5 n/a n/a3 3 n/a n/a n/a-
toLocaleString()1.5 1.55.5 5.56.0 6.05.5 5.5 n/a n/a3 3 n/a n/a n/aWarning
toPrecision()1.5 1.55.5 5.56.0 6.05.5 5.5 n/a n/a3 3 n/a n/a n/a-
toSource()1.3 1.3 n/a4.06 4.06 n/a n/a n/a3 3 n/a n/a n/a-
toString()1.1 1.11.0 1.03.0 3.03.02 3.023.0 3.0 n/a2 2 n/a n/a n/a-
valueOf()1.1 1.13.0 3.03.0 3.04.0 4.0 n/a n/a2 2 n/a n/a n/a-

Cross-references:

ECMA 262 edition 2 - section - 4.3.20

ECMA 262 edition 2 - section - 10.1.5

ECMA 262 edition 2 - section - 15.7

ECMA 262 edition 3 - section - 4.3.21

ECMA 262 edition 3 - section - 10.1.5

ECMA 262 edition 3 - section - 15.7

Wrox Instant JavaScript - page - 33