Number

 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 对象主要用于:

  • 如果参数无法被转换为数字,则返回 NaN
  • 在非构造器上下文中 (如:没有 new 操作符),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 the Object.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^532^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 新增了方法和属性: EPSILON, isFinite, isInteger, isNaN, parseFloat, parseInt
ECMAScript Latest Draft (ECMA-262)
Number
Draft

浏览器兼容性

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
NumberChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
EPSILONChrome Full support 34Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 21Safari Full support 9WebView Android Full support ≤37Chrome Android Full support 34Firefox Android Full support 25Opera Android Full support 21Safari iOS Full support 9Samsung Internet Android Full support 2.0nodejs Full support 0.12
MAX_SAFE_INTEGERChrome Full support 34Edge Full support 12Firefox Full support 31IE No support NoOpera Full support YesSafari Full support 9WebView Android Full support YesChrome Android Full support 34Firefox Android Full support 31Opera Android Full support YesSafari iOS Full support 9Samsung Internet Android Full support Yesnodejs Full support 0.12
MAX_VALUEChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 4Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
MIN_SAFE_INTEGERChrome Full support 34Edge Full support 12Firefox Full support 31IE No support NoOpera Full support YesSafari Full support 9WebView Android Full support YesChrome Android Full support 34Firefox Android Full support 31Opera Android Full support YesSafari iOS Full support 9Samsung Internet Android Full support Yesnodejs Full support 0.12
MIN_VALUEChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 4Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
NaNChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 4Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
NEGATIVE_INFINITYChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 4Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
POSITIVE_INFINITYChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 4Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
isFiniteChrome Full support 19Edge Full support 12Firefox Full support 16IE No support NoOpera Full support 15Safari Full support 9WebView Android Full support YesChrome Android Full support 25Firefox Android Full support 16Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support 0.10
isIntegerChrome Full support 34Edge Full support 12Firefox Full support 16IE No support NoOpera Full support 21Safari Full support YesWebView Android Full support ≤37Chrome Android Full support 34Firefox Android Full support 16Opera Android Full support 21Safari iOS Full support YesSamsung Internet Android Full support 2.0nodejs Full support 0.12
isNaNChrome Full support 25Edge Full support 12Firefox Full support 15IE No support NoOpera Full support YesSafari Full support 9WebView Android Full support YesChrome Android Full support 25Firefox Android Full support 15Opera Android Full support YesSafari iOS Full support 9Samsung Internet Android Full support Yesnodejs Full support 0.10
isSafeIntegerChrome Full support 34Edge Full support 12Firefox Full support 32IE No support NoOpera Full support 21Safari Full support 10WebView Android Full support ≤37Chrome Android Full support 34Firefox Android Full support 32Opera Android Full support 21Safari iOS Full support YesSamsung Internet Android Full support 2.0nodejs Full support 0.12
parseFloatChrome Full support 34Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 21Safari Full support 9WebView Android Full support ≤37Chrome Android Full support 34Firefox Android Full support 25Opera Android Full support 21Safari iOS Full support 9Samsung Internet Android Full support 2.0nodejs Full support 0.12
parseIntChrome Full support 34Edge Full support 12Firefox Full support 25IE No support NoOpera Full support 21Safari Full support 9WebView Android Full support ≤37Chrome Android Full support 34Firefox Android Full support 25Opera Android Full support 21Safari iOS Full support 9Samsung Internet Android Full support 2.0nodejs Full support 0.12
prototypeChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
toExponentialChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 5.5Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
toFixedChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 5.5Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
toInteger
DeprecatedNon-standard
Chrome No support NoEdge No support NoFirefox No support 16 — 32IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoFirefox Android No support 16 — 32Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support Nonodejs No support No
toLocaleStringChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 5Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
toPrecisionChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 5.5Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
toSource
Non-standard
Chrome No support NoEdge No support NoFirefox Full support 1IE No support NoOpera No support NoSafari No support NoWebView Android No support NoChrome Android No support NoFirefox Android Full support 4Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support Nonodejs No support No
toStringChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 3Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs Full support Yes
valueOfChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 4Opera Full support YesSafari Full support YesWebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support 1.0nodejs 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.

参阅