When the Number() constructor is called as a function, it will perform a type conversion.
The following values are yielded as a result of calling Number() as a function:
Value | Result |
---|---|
No value | 0 |
Undefined | Returns NaN |
Null | 0 |
Boolean false | 0 |
Boolean true | 1 |
Number | No conversion, the input value is returned unchanged. |
Non numeric string | NaN |
Numeric string | The numeric value rounded down if the number of digits exceeds the numeric accuracy specified by Number.MAX_VALUE. |
Object | Internally, a conversion to one of the primitive types happens followed by a conversion from that type to a number. Some objects will return a number that is readily usable; others will return something that cannot be converted and NaN will result. |
The result is a number value that is equivalent to the value of the passed in argument. If the argument is omitted the value 0 is returned.
When converting strings to numbers, the number of digits in the numeric string is significant. If it exceeds the accuracy that the numeric storage can cope with, the value needs to be rounded before conversion. This is an area where the implementations are notoriously weak. MSIE apparently does a better job than Netscape. However, both are undergoing revision and its possible that the new versions of each will cope better than the older ones did.
ECMA 262 edition 2 - section - 15.1.3.6
ECMA 262 edition 2 - section - 15.7.1
ECMA 262 edition 3 - section - 15.7.1
Wrox Instant JavaScript - page - 36
Prev | Home | Next |
Number() | Up | Number.Class |
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. |