Общие сведения
Number представляет собой JavaScript объект-обертку для работы с числовыми значениями. Объект Number создается вызовом конструктора Number().
Конструктор
new Number(значение);
Параметры
-
значение - Числовое значение, которое примет объект после создания.
Описание
Несколько замечаний относительно использования объекта Number:
- Если агрумент не может быть преобразован в число, то вернется спечиальное значение
NaN. - Объект
Numberможет использоваться и без вызова констуктора New.
Свойства
-
Number.EPSILON - Наименьший интервал между двумя представимыми в памяти числами.
-
Number.MAX_VALUE - Наибольшее положительно представимое в памяти число.
-
Number.MIN_VALUE - Наименьшее представимое в памяти положительное число, которое еще не равно нулю.
-
Number.NaN - Специальное значение, которое не является числовым (сокращение от англ. "Not a Number").
-
Number.NEGATIVE_INFINITY - Специальное значение, обозначающее отрицательную бесконечность. Может возвращатся при переполнении.
-
Number.POSITIVE_INFINITY - Специальное значение, обозначающее положительную бесконечность. Может возвращатся при переполнении.
-
Number.prototype - Прототип, обеспечивающий доступ к дополнительным свойствам объекта .
Методы
Number instances, see Methods of Number instances.-
Number.isNaN() - Определяет, имеет ли переданный аргумент значение NaN.
-
Number.isFinite() - Определяет явлется ли значение конечным.
-
Number.isInteger() - Determine whether the type of the passed value is "number" and the passed value is an integer.
-
Number.toInteger() - Evaluate the passed value and convert it to an integer (or Infinity).
-
Number.parseFloat() -
The value is the same as
parseFloatof the global object. -
Number.parseInt() -
The value is the same as
parseIntof the global object.
Number instances
All Number instances inherit from Number.prototype. The prototype object of the Number constructor can be modified to affect all Number instances.
Methods
-
Number.prototype.toExponential() - Returns a string representing the number in exponential notation.
-
Number.prototype.toFixed() - Returns a string representing the number in fixed-point notation.
-
Number.prototype.toLocaleString() -
Returns a string with a language sensitive representation of this number. Overrides the
Object.prototype.toLocaleString()method. -
Number.prototype.toPrecision() - Returns a string representing the number to a specified precision in fixed-point or exponential notation.
-
Number.prototype.toSource() -
Returns an object literal representing the specified Number object; you can use this value to create a new object. Overrides the
Object.prototype.toSource()method. -
Number.prototype.toString() -
Returns a string representing the specified object. Overrides the
Object.prototype.toString()method. -
Number.prototype.valueOf() -
Returns the primitive value of the specified object. Overrides the
Object.prototype.valueOf()method.
Examples
Example: 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:
var biggestNum = Number.MAX_VALUE; var smallestNum = Number.MIN_VALUE; var infiniteNum = Number.POSITIVE_INFINITY; var negInfiniteNum = Number.NEGATIVE_INFINITY; var notANum = Number.NaN;
Example: Integer range for Number
The following example shows minimum and maximum integer values that can be represented as Number object (for details, refer to EcmaScript standard, chapter 8.5 The Number Type):
var biggestInt = 9007199254740992; var smallestInt = -9007199254740992;
When parsing data that has been serialized to JSON, integer values falling out of this range can be expected to become corrupted when JSON parser coerces them to Number type. Using String instead is a possible workaround.
Example: Using Number to convert a Date object
The following example converts the Date object to a numerical value using Number as a function:
var d = new Date("December 17, 1995 03:24:00");
print(Number(d));
This displays "819199440000".
Specifications
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 1st Edition. Implemented in JavaScript 1.1 | Standard | Initial definition. |
| ECMAScript Language Specification 5.1th Edition (ECMA-262) | Standard | |
| ECMAScript Language Specification 6th Edition (ECMA-262) | Draft | New methods and properties added (EPSILON, isFinite, isInteger, isNaN, parseFloat, parseInt) |
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |