JavaScript 的 Number
对象是经过封装的能让你处理数字值的对象。Number
对象由 Number()
构造器创建。
JavaScript的Number
类型为双精度IEEE 754 64位浮点类型。
最近出了stage3BigInt
任意精度数字类型,已经进入stage3规范
语法
new Number(value); var a = new Number('123'); // a === 123 is false var b = Number('123'); // b === 123 is true a instanceof Number; // is true b instanceof Number; // is false
参数
-
value
- 被创建对象的数字值。
描述
Number
对象主要用于:
属性
-
Number.EPSILON
- 两个可表示(representable)数之间的最小间隔。
-
Number.MAX_SAFE_INTEGER
-
JavaScript 中最大的安全整数 (
253 - 1
)。 -
Number.MAX_VALUE
-
能表示的最大正数。最小的负数是
-MAX_VALUE
。 -
Number.MIN_SAFE_INTEGER
-
JavaScript 中最小的安全整数 (
-(253 - 1)
). -
Number.MIN_VALUE
-
能表示的最小正数即最接近 0 的正数 (实际上不会变成 0)。最大的负数是
-MIN_VALUE
。 -
Number.NaN
- 特殊的“非数字”值。
-
Number.NEGATIVE_INFINITY
- 特殊的负无穷大值,在溢出时返回该值。
-
Number.POSITIVE_INFINITY
- 特殊的正无穷大值,在溢出时返回该值。
-
Number.prototype
- Number 对象上允许的额外属性。
方法
-
Number.isNaN()
- 确定传递的值是否是 NaN。
-
Number.isFinite()
- 确定传递的值类型及本身是否是有限数。
-
Number.isInteger()
- 确定传递的值类型是“number”,且是整数。
-
Number.isSafeInteger()
-
确定传递的值是否为安全整数 ( -
(253 - 1)
至253 - 1之间
)。 -
Number.toInteger()
- 计算传递的值并将其转换为整数 (或无穷大)。
-
Number.parseFloat()
-
和全局对象
parseFloat()
一样。 -
Number.parseInt()
-
和全局对象
parseInt()
一样。
Number
实例
所有 Number
实例都继承自 Number.prototype
。被修改的 Number
构造器的原型对象对全部 Number
实例都生效。
方法
-
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 theObject.prototype.toSource()
method. -
Number.prototype.toString()
-
Returns a string representing the specified object in the specified radix (base). Overrides the
Object.prototype.toString()
method. -
Number.prototype.valueOf()
-
Returns the primitive value of the specified object. Overrides the
Object.prototype.valueOf()
method.
示例
使用 Number 对象给数字变量赋值
下例使用 Number
对象的属性给几个数字变量赋值:
var biggestNum = Number.MAX_VALUE; var smallestNum = Number.MIN_VALUE; var infiniteNum = Number.POSITIVE_INFINITY; var negInfiniteNum = Number.NEGATIVE_INFINITY; var notANum = Number.NaN;
整数类型的范围
JavaScript 能够准确表示的整数范围在-2^53
到2^53
之间(不含两个端点),超过这个范围,无法精确表示这个整数。 (详情请参阅 ECMAScript standard, chapter 6.1.6 The Number Type):
var biggestInt = Number.MAX_SAFE_INTEGER; //9007199254740991 var smallestInt = Number.MIN_SAFE_INTEGER; //-9007199254740991
在解析序列化的JSON时,如果JSON解析器将它们强制转换为Number类型,那么超出此范围的整数值可能会被破坏。在工作中使用String
类型代替,是一个可行的解决方案。
使用 Number
转换 Date
对象
下例使用 Number 作为函数来转换 Date
对象为数字值:
var d = new Date("December 17, 1995 03:24:00"); print(Number(d));
这将输出 "819199440000"。
转换数字字符串为数字
Number('123') // 123
Number('12.3') // 12.3
Number('12.00') // 12
Number('123e-1') // 12.3
Number('') // 0
Number(null) // 0
Number('0x11') // 17
Number('0b11') // 3
Number('0o11') // 9
Number('foo') // NaN
Number('100a') // NaN
Number('-Infinity') //-Infinity
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | 初始定义。 实现于 JavaScript 1.1. |
ECMAScript 5.1 (ECMA-262) Number |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) Number |
Standard | 新增了方法和属性: parseInt |
ECMAScript Latest Draft (ECMA-262) Number |
Draft |
浏览器兼容性
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-dataand send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Number |
Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
EPSILON |
Chrome Full support 34 | Edge Full support 12 | Firefox Full support 25 | IE No support No | Opera Full support 21 | Safari Full support 9 | WebView Android Full support ≤37 | Chrome Android Full support 34 | Firefox Android Full support 25 | Opera Android Full support 21 | Safari iOS Full support 9 | Samsung Internet Android Full support 2.0 | nodejs Full support 0.12 |
MAX_SAFE_INTEGER |
Chrome Full support 34 | Edge Full support 12 | Firefox Full support 31 | IE No support No | Opera Full support Yes | Safari Full support 9 | WebView Android Full support Yes | Chrome Android Full support 34 | Firefox Android Full support 31 | Opera Android Full support Yes | Safari iOS Full support 9 | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
MAX_VALUE |
Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 4 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
MIN_SAFE_INTEGER |
Chrome Full support 34 | Edge Full support 12 | Firefox Full support 31 | IE No support No | Opera Full support Yes | Safari Full support 9 | WebView Android Full support Yes | Chrome Android Full support 34 | Firefox Android Full support 31 | Opera Android Full support Yes | Safari iOS Full support 9 | Samsung Internet Android Full support Yes | nodejs Full support 0.12 |
MIN_VALUE |
Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 4 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
NaN |
Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 4 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
NEGATIVE_INFINITY |
Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 4 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
POSITIVE_INFINITY |
Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 4 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
isFinite |
Chrome Full support 19 | Edge Full support 12 | Firefox Full support 16 | IE No support No | Opera Full support 15 | Safari Full support 9 | WebView Android Full support Yes | Chrome Android Full support 25 | Firefox Android Full support 16 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes | nodejs Full support 0.10 |
isInteger |
Chrome Full support 34 | Edge Full support 12 | Firefox Full support 16 | IE No support No | Opera Full support 21 | Safari Full support Yes | WebView Android Full support ≤37 | Chrome Android Full support 34 | Firefox Android Full support 16 | Opera Android Full support 21 | Safari iOS Full support Yes | Samsung Internet Android Full support 2.0 | nodejs Full support 0.12 |
isNaN |
Chrome Full support 25 | Edge Full support 12 | Firefox Full support 15 | IE No support No | Opera Full support Yes | Safari Full support 9 | WebView Android Full support Yes | Chrome Android Full support 25 | Firefox Android Full support 15 | Opera Android Full support Yes | Safari iOS Full support 9 | Samsung Internet Android Full support Yes | nodejs Full support 0.10 |
isSafeInteger |
Chrome Full support 34 | Edge Full support 12 | Firefox Full support 32 | IE No support No | Opera Full support 21 | Safari Full support 10 | WebView Android Full support ≤37 | Chrome Android Full support 34 | Firefox Android Full support 32 | Opera Android Full support 21 | Safari iOS Full support Yes | Samsung Internet Android Full support 2.0 | nodejs Full support 0.12 |
parseFloat |
Chrome Full support 34 | Edge Full support 12 | Firefox Full support 25 | IE No support No | Opera Full support 21 | Safari Full support 9 | WebView Android Full support ≤37 | Chrome Android Full support 34 | Firefox Android Full support 25 | Opera Android Full support 21 | Safari iOS Full support 9 | Samsung Internet Android Full support 2.0 | nodejs Full support 0.12 |
parseInt |
Chrome Full support 34 | Edge Full support 12 | Firefox Full support 25 | IE No support No | Opera Full support 21 | Safari Full support 9 | WebView Android Full support ≤37 | Chrome Android Full support 34 | Firefox Android Full support 25 | Opera Android Full support 21 | Safari iOS Full support 9 | Samsung Internet Android Full support 2.0 | nodejs Full support 0.12 |
prototype |
Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
toExponential |
Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 5.5 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
toFixed |
Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 5.5 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
toInteger
|
Chrome No support No | Edge No support No | Firefox No support 16 — 32 | IE No support No | Opera No support No | Safari No support No | WebView Android No support No | Chrome Android No support No | Firefox Android No support 16 — 32 | Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No | nodejs No support No |
toLocaleString |
Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 5 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
toPrecision |
Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 5.5 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
toSource
|
Chrome No support No | Edge No support No | Firefox Full support 1 | IE No support No | Opera No support No | Safari No support No | WebView Android No support No | Chrome Android No support No | Firefox Android Full support 4 | Opera Android No support No | Safari iOS No support No | Samsung Internet Android No support No | nodejs No support No |
toString |
Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 3 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
valueOf |
Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 4 | Opera Full support Yes | Safari Full support Yes | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Legend
- Full support
- Full support
- No support
- No support
- Non-standard. Expect poor cross-browser support.
- Non-standard. Expect poor cross-browser support.
- Deprecated. Not for use in new websites.
- Deprecated. Not for use in new websites.
参阅
NaN
- 全局对象
Math
- Integers with arbitrary precision:
BigInt
- Number type in details