Availability: |
| ||||||||
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.
The Number object provides a collection of static constant values by way of properties belonging to the integral Number object. Because the mathematical mechanisms of any application tend to be provided by the operating system, you should find that between different browsers on any particular platform, the values that these constants yield will be very consistent.
The ECMA standard lays down strict values for these properties and in general the browser manufacturers try to comply, but there is always the possibility that an implementation may use a non-compliant calculation.
However, it may not be quite so reliable across platforms. You might enumerate one of these constants as you are authoring and then hard code that value into your script. When that script is executed on another platform, even in the same browser, the internal numeric support may yield a different value.
You should always refer to the static constants using their symbolic names and not define them yourself, unless you are certain that the script is running on a platform that does not already define the constant value.
See also: | Constant, Limits, Native object, Number.Class, Number.prototype, Object object |
Property | JavaScript | JScript | N | IE | Opera | NES | ECMA | DOM | CSS | HTML | Notes |
---|---|---|---|---|---|---|---|---|---|---|---|
constructor | 1.1 ![]() | 1.0 ![]() | 3.0 ![]() | 3.02 ![]() | ![]() | ![]() | 2 ![]() | ![]() | ![]() | ![]() | - |
prototype | 1.1 ![]() | 1.0 ![]() | 3.0 ![]() | 3.02 ![]() | 3.0 ![]() | 2.0 ![]() | 2 ![]() | ![]() | ![]() | ![]() | DontDelete, DontEnum. |
Method | JavaScript | JScript | N | IE | Opera | NES | ECMA | DOM | CSS | HTML | Notes |
---|---|---|---|---|---|---|---|---|---|---|---|
toExponential() | 1.5 ![]() | 5.5 ![]() | 6.0 ![]() | 5.5 ![]() | ![]() | ![]() | 3 ![]() | ![]() | ![]() | ![]() | - |
toFixed() | 1.5 ![]() | 5.5 ![]() | 6.0 ![]() | 5.5 ![]() | ![]() | ![]() | 3 ![]() | ![]() | ![]() | ![]() | - |
toLocaleString() | 1.5 ![]() | 5.5 ![]() | 6.0 ![]() | 5.5 ![]() | ![]() | ![]() | 3 ![]() | ![]() | ![]() | ![]() | ![]() |
toPrecision() | 1.5 ![]() | 5.5 ![]() | 6.0 ![]() | 5.5 ![]() | ![]() | ![]() | 3 ![]() | ![]() | ![]() | ![]() | - |
toSource() | 1.3 ![]() | ![]() | 4.06 ![]() | ![]() | ![]() | ![]() | 3 ![]() | ![]() | ![]() | ![]() | - |
toString() | 1.1 ![]() | 1.0 ![]() | 3.0 ![]() | 3.02 ![]() | 3.0 ![]() | ![]() | 2 ![]() | ![]() | ![]() | ![]() | - |
valueOf() | 1.1 ![]() | 3.0 ![]() | 3.0 ![]() | 4.0 ![]() | ![]() | ![]() | 2 ![]() | ![]() | ![]() | ![]() | - |
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
Prev | Home | Next |
Number formats (.) | Up | Number() |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |