21.1 Number Objects
21.1.1 The Number Constructor
The Number
- is %Number%.
- is the initial value of the
"Number" property of theglobal object . - creates and initializes a new Number object when called as a
constructor . - performs a type conversion when called as a function rather than as a
constructor . - may be used as the value of an
extends
clause of a class definition. Subclassconstructors that intend to inherit the specified Number behaviour must include asuper
call to the Numberconstructor to create and initialize the subclass instance with a [[NumberData]] internal slot.
21.1.1.1 Number ( value )
This function performs the following steps when called:
- If value is present, then
- Let prim be ?
ToNumeric (value). - If prim
is a BigInt , let n be𝔽 (ℝ (prim)). - Otherwise, let n be prim.
- Let prim be ?
- Else,
- Let n be
+0 𝔽.
- Let n be
- If NewTarget is
undefined , return n. - Let O be ?
OrdinaryCreateFromConstructor (NewTarget,"%Number.prototype%" , « [[NumberData]] »). - Set O.[[NumberData]] to n.
- Return O.
21.1.2 Properties of the Number Constructor
The Number
- has a [[Prototype]] internal slot whose value is
%Function.prototype% . - has the following properties:
21.1.2.1 Number.EPSILON
The value of Number.EPSILON
is the
This property has the attributes { [[Writable]]:
21.1.2.2 Number.isFinite ( number )
This function performs the following steps when called:
- If number
is not a Number , returnfalse . - If number is not
finite , returnfalse . - Otherwise, return
true .
21.1.2.3 Number.isInteger ( number )
This function performs the following steps when called:
- Return
IsIntegralNumber (number).
21.1.2.4 Number.isNaN ( number )
This function performs the following steps when called:
- If number
is not a Number , returnfalse . - If number is
NaN , returntrue . - Otherwise, return
false .
This function differs from the global isNaN function (
21.1.2.5 Number.isSafeInteger ( number )
An
This function performs the following steps when called:
- If
IsIntegralNumber (number) istrue , then - Return
false .
21.1.2.6 Number.MAX_SAFE_INTEGER
Due to rounding behaviour necessitated by precision limitations of Number.MAX_SAFE_INTEGER
is shared with at least one other 9007199254740992
and 9007199254740993
evaluate to the Number value
The value of Number.MAX_SAFE_INTEGER
is
This property has the attributes { [[Writable]]:
21.1.2.7 Number.MAX_VALUE
The value of Number.MAX_VALUE
is the largest positive
This property has the attributes { [[Writable]]:
21.1.2.8 Number.MIN_SAFE_INTEGER
Due to rounding behaviour necessitated by precision limitations of Number.MIN_SAFE_INTEGER
is shared with at least one other -9007199254740992
and -9007199254740993
evaluate to the Number value
The value of Number.MIN_SAFE_INTEGER
is
This property has the attributes { [[Writable]]:
21.1.2.9 Number.MIN_VALUE
The value of Number.MIN_VALUE
is the smallest positive value of the
In the Number.MIN_VALUE
must be the smallest non-zero positive value that can actually be represented by the implementation.
This property has the attributes { [[Writable]]:
21.1.2.10 Number.NaN
The value of Number.NaN
is
This property has the attributes { [[Writable]]:
21.1.2.11 Number.NEGATIVE_INFINITY
The value of Number.NEGATIVE_INFINITY
is
This property has the attributes { [[Writable]]:
21.1.2.12 Number.parseFloat ( string )
The initial value of the
21.1.2.13 Number.parseInt ( string, radix )
The initial value of the
21.1.2.14 Number.POSITIVE_INFINITY
The value of Number.POSITIVE_INFINITY
is
This property has the attributes { [[Writable]]:
21.1.2.15 Number.prototype
The initial value of Number.prototype
is the
This property has the attributes { [[Writable]]:
21.1.3 Properties of the Number Prototype Object
The Number prototype object:
- is %Number.prototype%.
- is an
ordinary object . - is itself a Number object; it has a [[NumberData]] internal slot with the value
+0 𝔽. - has a [[Prototype]] internal slot whose value is
%Object.prototype% .
Unless explicitly stated otherwise, the methods of the Number prototype object defined below are not generic and the
The phrase “this Number value” within the specification of a method refers to the result returned by calling the abstract operation
21.1.3.1 Number.prototype.constructor
The initial value of Number.prototype.constructor
is
21.1.3.2 Number.prototype.toExponential ( fractionDigits )
This method returns a String containing this Number value represented in decimal exponential notation with one digit before the significand's decimal point and fractionDigits digits after the significand's decimal point. If fractionDigits is
It performs the following steps when called:
- Let x be ?
ThisNumberValue (this value). - Let f be ?
ToIntegerOrInfinity (fractionDigits). Assert : If fractionDigits isundefined , then f is 0.- If x is not
finite , returnNumber::toString (x, 10). - If f < 0 or f > 100, throw a
RangeError exception. - Set x to
ℝ (x). - Let s be the empty String.
- If x < 0, then
- Set s to
"-" . - Set x to -x.
- Set s to
- If x = 0, then
- Let m be the String value consisting of f + 1 occurrences of the code unit 0x0030 (DIGIT ZERO).
- Let e be 0.
- Else,
- If fractionDigits is not
undefined , then- Let e and n be
integers such that 10f ≤ n < 10f + 1 and for which n × 10e - f - x is as close to zero as possible. If there are two such sets of e and n, pick the e and n for which n × 10e - f is larger.
- Let e and n be
- Else,
- Let e, n, and ff be
integers such that ff ≥ 0, 10ff ≤ n < 10ff + 1,𝔽 (n × 10e - ff) is𝔽 (x), and ff is as small as possible. Note that the decimal representation of n has ff + 1 digits, n is not divisible by 10, and the least significant digit of n is not necessarily uniquely determined by these criteria. - Set f to ff.
- Let e, n, and ff be
- Let m be the String value consisting of the digits of the decimal representation of n (in order, with no leading zeroes).
- If fractionDigits is not
- If f ≠ 0, then
- Let a be the first code unit of m.
- Let b be the other f code units of m.
- Set m to the
string-concatenation of a,"." , and b.
- If e = 0, then
- Let c be
"+" . - Let d be
"0" .
- Let c be
- Else,
- If e > 0, then
- Let c be
"+" .
- Let c be
- Else,
Assert : e < 0.- Let c be
"-" . - Set e to -e.
- Let d be the String value consisting of the digits of the decimal representation of e (in order, with no leading zeroes).
- If e > 0, then
- Set m to the
string-concatenation of m,"e" , c, and d. - Return the
string-concatenation of s and m.
For implementations that provide more accurate conversions than required by the rules above, it is recommended that the following alternative version of step
- Let e, n, and f be
integers such that f ≥ 0, 10f ≤ n < 10f + 1,𝔽 (n × 10e - f) is𝔽 (x), and f is as small as possible. If there are multiple possibilities for n, choose the value of n for which𝔽 (n × 10e - f) is closest in value to𝔽 (x). If there are two such possible values of n, choose the one that is even.
21.1.3.3 Number.prototype.toFixed ( fractionDigits )
This method returns a String containing this Number value represented in decimal fixed-point notation with fractionDigits digits after the decimal point. If fractionDigits is
It performs the following steps when called:
- Let x be ?
ThisNumberValue (this value). - Let f be ?
ToIntegerOrInfinity (fractionDigits). Assert : If fractionDigits isundefined , then f is 0.- If f is not
finite , throw aRangeError exception. - If f < 0 or f > 100, throw a
RangeError exception. - If x is not
finite , returnNumber::toString (x, 10). - Set x to
ℝ (x). - Let s be the empty String.
- If x < 0, then
- Set s to
"-" . - Set x to -x.
- Set s to
- If x ≥ 1021, then
- Else,
- Let n be an
integer for which n / 10f - x is as close to zero as possible. If there are two such n, pick the larger n. - If n = 0, let m be
"0" . Otherwise, let m be the String value consisting of the digits of the decimal representation of n (in order, with no leading zeroes). - If f ≠ 0, then
- Let k be the length of m.
- If k ≤ f, then
- Let z be the String value consisting of f + 1 - k occurrences of the code unit 0x0030 (DIGIT ZERO).
- Set m to the
string-concatenation of z and m. - Set k to f + 1.
- Let a be the first k - f code units of m.
- Let b be the other f code units of m.
- Set m to the
string-concatenation of a,"." , and b.
- Let n be an
- Return the
string-concatenation of s and m.
The output of toFixed
may be more precise than toString
for some values because toString only prints enough significant digits to distinguish the number from adjacent Number values. For example,
(1000000000000000128).toString()
returns
(1000000000000000128).toFixed(0)
returns
21.1.3.4 Number.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )
An ECMAScript implementation that includes the ECMA-402 Internationalization API must implement this method as specified in the ECMA-402 specification. If an ECMAScript implementation does not include the ECMA-402 API the following specification of this method is used:
This method produces a String value that represents this Number value formatted according to the conventions of the toString
.
The meanings of the optional parameters to this method are defined in the ECMA-402 specification; implementations that do not include ECMA-402 support must not use those parameter positions for anything else.
21.1.3.5 Number.prototype.toPrecision ( precision )
This method returns a String containing this Number value represented either in decimal exponential notation with one digit before the significand's decimal point and
It performs the following steps when called:
- Let x be ?
ThisNumberValue (this value). - If precision is
undefined , return !ToString (x). - Let p be ?
ToIntegerOrInfinity (precision). - If x is not
finite , returnNumber::toString (x, 10). - If p < 1 or p > 100, throw a
RangeError exception. - Set x to
ℝ (x). - Let s be the empty String.
- If x < 0, then
- Set s to the code unit 0x002D (HYPHEN-MINUS).
- Set x to -x.
- If x = 0, then
- Let m be the String value consisting of p occurrences of the code unit 0x0030 (DIGIT ZERO).
- Let e be 0.
- Else,
- Let e and n be
integers such that 10p - 1 ≤ n < 10p and for which n × 10e - p + 1 - x is as close to zero as possible. If there are two such sets of e and n, pick the e and n for which n × 10e - p + 1 is larger. - Let m be the String value consisting of the digits of the decimal representation of n (in order, with no leading zeroes).
- If e < -6 or e ≥ p, then
Assert : e ≠ 0.- If p ≠ 1, then
- Let a be the first code unit of m.
- Let b be the other p - 1 code units of m.
- Set m to the
string-concatenation of a,"." , and b.
- If e > 0, then
- Let c be the code unit 0x002B (PLUS SIGN).
- Else,
Assert : e < 0.- Let c be the code unit 0x002D (HYPHEN-MINUS).
- Set e to -e.
- Let d be the String value consisting of the digits of the decimal representation of e (in order, with no leading zeroes).
- Return the
string-concatenation of s, m, the code unit 0x0065 (LATIN SMALL LETTER E), c, and d.
- Let e and n be
- If e = p - 1, return the
string-concatenation of s and m. - If e ≥ 0, then
- Set m to the
string-concatenation of the first e + 1 code units of m, the code unit 0x002E (FULL STOP), and the remaining p - (e + 1) code units of m.
- Set m to the
- Else,
- Set m to the
string-concatenation of the code unit 0x0030 (DIGIT ZERO), the code unit 0x002E (FULL STOP), -(e + 1) occurrences of the code unit 0x0030 (DIGIT ZERO), and the String m.
- Set m to the
- Return the
string-concatenation of s and m.
21.1.3.6 Number.prototype.toString ( [ radix ] )
The optional radix should be an
This method performs the following steps when called:
- Let x be ?
ThisNumberValue (this value). - If radix is
undefined , let radixMV be 10. - Else, let radixMV be ?
ToIntegerOrInfinity (radix). - If radixMV is not in the
inclusive interval from 2 to 36, throw aRangeError exception. - Return
Number::toString (x, radixMV).
This method is not generic; it throws a
The
21.1.3.7 Number.prototype.valueOf ( )
- Return ?
ThisNumberValue (this value).
21.1.3.7.1 ThisNumberValue ( value )
The abstract operation ThisNumberValue takes argument value (an
- If value
is a Number , return value. - If value
is an Object and value has a [[NumberData]] internal slot, then- Let n be value.[[NumberData]].
Assert : nis a Number .- Return n.
- Throw a
TypeError exception.
21.1.4 Properties of Number Instances
Number instances are