Number object
Represents a numeric value as an object.
The Number object supports numeric constants and methods to help work with numbers.
In JavaScript, primitive numeric values (e.g. 1, 2, 23.12, -2.34) differ from the Number object, their data type is number not object. When you use numbers in JavaScript, you work with primitive numeric values, not instances of the Number object.
The JavaScript interpreter does not use the Number object for implicit data type conversions (when a non-numeric value needs to be converted to numeric, for example 2 * "32"), but the Number object can be used as a function to convert a value to a primitive numeric value explicitly. See Example 3 for details.
When the JavaScript interpreter needs to convert a primitive numeric value to an object, it uses the Number object. Example 5 illustrates this mechanism.
Syntax:
Creating a new Number object:
The type of number can be a number, string or boolean.
If number cannot be converted into a numeric value, num will be NaN.
In case of string values, leading and trailing whitespaces are allowed. If a string value starts with "0x" or "0X", the base is hexadecimal, otherwise it is decimal.
If any part of the string cannot be parsed into a number, the returned value is NaN.
Creating primitive numeric values:
In JavaScript, integers can be represented in decimal, hexadecimal and octal formats, while floating point numbers and numbers in exponential notation must be in decimal format. Hexadecimal integers begin with 0x or 0X followed by one or more hexadecimal digits (0–9, A–F, a-f). Octal integers begin with 0 followed by one or more octal digits (0-7). If an integer begins with 0 and contains the digit 8 or 9, it is a decimal number.
Getting a predefined constant, such as POSITIVE_INFINITY:
Converting values to numeric values:
If the value cannot be converted into a numeric value, the returned value is NaN.
In case of string values, leading and trailing whitespaces are allowed. If a string value starts with "0x" or "0X", the base is hexadecimal, otherwise it is decimal.
Exponential notations are supported for decimal strings.
If the whole string cannot be parsed into a number, the returned value is NaN.
Note that the unary addition operator is identical to the Number constructor:
There are other methods to convert string values to numbers:
Converting numbers to string literals:
Using methods to add new functionality to the Number object:
|
Members:
The Number object inherits from the Number.prototype and Function.prototype objects.
The following lists only contain the members of the Number and Number.prototype objects.
(*) - The method is inherited from the Number.prototype.
Properties:
Property | Support | Description | |||||
---|---|---|---|---|---|---|---|
MAX_VALUE | Returns the largest possible number that JavaScript can represent. The MAX_VALUE property is static, it cannot be accessed from an instance of the Number object, only Number.MAX_VALUE is allowed. Be careful, it does not mean that JavaScript can work with numbers between (-1 * Number.MAX_VALUE, Number.MAX_VALUE) correctly. In a 32-bit system the real limit is (-1 * 2^53, 2^53). See the Infinity global property for details and examples. | ||||||
MIN_VALUE | Returns the smallest positive floating point number, that JavaScript can represent (about 2^-1074). The MIN_VALUE property is static, it cannot be accessed from an instance of the Number object, only Number.MIN_VALUE is allowed. | ||||||
NaN | Same as the global NaN property. A constant value that indicates that the value of an expression is 'Not-A-Number'. The NaN property is static, it cannot be accessed from an instance of the Number object, only Number.NaN is allowed. If a method (such as parseFloat, parseInt) returns NaN that means that the specified parameter cannot be parsed as a number. | ||||||
NEGATIVE_INFINITY | Returns negative infinity (any number smaller than MIN_VALUE). Same as the -Infinity global property, see this for details and examples. The NEGATIVE_INFINITY property is static, it cannot be accessed from an instance of the Number object, only Number.NEGATIVE_INFINITY is allowed. | ||||||
POSITIVE_INFINITY | Returns positive infinity (any number greater than MAX_VALUE). Same as the Infinity global property, see this for details and examples The POSITIVE_INFINITY property is static, it cannot be accessed from an instance of the Number object, only Number.POSITIVE_INFINITY is allowed. | ||||||
prototype | Returns a reference to the Number.prototype object. The Number.prototype object allows adding properties and methods to the Number object that can be used with instances of the Number object, like any predefined property or method. The prototype property is static, it cannot be accessed from an instance of the Number object, only Number.prototype is allowed. |
Methods:
Method | Support | Description | |||||||
---|---|---|---|---|---|---|---|---|---|
toExponential ([fractionDigits])* |
Returns a string that represents the current number in exponential notation.
The string contains one digit before the decimal point and a limited number of digits to the right of the decimal point. At the end of the string a signed integer is displayed after the 'e' letter. This signed integer specifies an exponent of ten indicating the multiplier.
|
||||||||
toFixed ([fractionDigits])* |
Returns a string that represents the current number with a limited number of digits to the right of the decimal point.
|
||||||||
toLocaleString ( )* | Returns a string that represents the current number in a localized form. For example, it may change the decimal point to a comma, because in some languages the numbers are displayed in that form. | ||||||||
toPrecision ([precision])* |
Returns a string that represents the current number with the specified number of digits in fixed-point or exponential notation.
|
||||||||
toSource ( )* | Returns a string representing the source code of the current number. | ||||||||
toString ([radix])* |
Returns a string that represents the current number in the specified base.
When a number needs to be converted to a string, the JavaScript interpreter automatically calls its toString method.
If you need to convert a string of the given base to an integer, use the parseInt method.
|
||||||||
valueOf ( )* | Returns the current Number object as a primitive numeric value. |
(*) - The method is inherited from the Number.prototype.
Examples:
Example 1:
How to get the largest number:
Example 2:
How to reduce the number of digits after the decimal point:
Example 3:
Converting values to primitive number values explicitly with the Number function:
Example 4:
Converting floating point values to integers:
Example 5:
Adding a new method to the Number object and calling it for a primitive numeric value:
|
External links:
User Contributed Comments