Number
JavaScript 的Number 对象是经过封装的能让你处理数字值的对象。Number对象由 Number()构造器创建。
语法
new Number(value);参数
value被创建对象的数字值。
描述
Number对象主要用于:
- 如果参数无法被转换为数字,则返回
NaN。
- 在非构造器上下文中 (如:没有
new操作符),Number能被用来执行类型转换。
属性
Number.EPSILON两个可表示(representable)数之间的最小间隔。
Number.MAX_SAFE_INTEGERJavaScript 中最大的安全整数 (253- 1)。
Number.MAX_VALUE能表示的最大正数。最小的负数是-MAX_VALUE。
Number.MIN_SAFE_INTEGERJavaScript 中最小的安全整数 (-(253- 1)).
Number.MIN_VALUE能表示的最小正数即最接近 0 的正数 (实际上不会变成 0)。最大的负数是-MIN_VALUE。
Number.NaN特殊的“非数字”值。
Number.NEGATIVE_INFINITY特殊的负无穷大值,在溢出时返回该值。
Number.POSITIVE_INFINITY特殊的正无穷大值,在溢出时返回改值。
Number.prototypeNumber 对象上允许的额外属性。
方法
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 specifiedNumberobject; 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 theObject.prototype.toString()method.
Number.prototype.valueOf()Returns the primitive value of the specified object. Overrides theObject.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;整数类型的范围
下例展示了Number对象所能表示的最大和最小整数 (详情请参阅 ECMAScript standard, chapter 8.5 The Number Type):
var biggestInt = 9007199254740991;
var smallestInt = -9007199254740991;在解析序列化的JSON时,如果JSON解析器将它们强制转换为Number类型,那么超出此范围的整数值可能会被破坏。在工作中使用String类型代替,是一个可行的解决方案。
使用 Number 转换 Date 对象
下例使用 Number 作为函数来转换Date对象为数字值:
var d = new Date('December 17, 1995 03:24:00');
console.log(Number(d));这将输出 "819199440000"。
转换数字字符串为数字
Number('123') // 123
Number('12.3') // 12.3
Number('') // 0
Number('0x11') // 17
Number('0b11') // 3
Number('0o11') // 9
Number('foo') // NaN
Number('100a') // NaN规范
Specification | Status | Comment |
|---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.1. |
ECMAScript 5.1 (ECMA-262)The definition of 'Number' in that specification. | Standard | |
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'Number' in that specification. | Standard | New methods and properties added: EPSILON, isFinite, isInteger, isNaN, parseFloat, parseInt |
ECMAScript Latest Draft (ECMA-262)The definition of 'Number' in that specification. | Living Standard | |
浏览器兼容性
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|---|
Basic Support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
Feature | Android | Chrome for Android | Edge mobile | Firefox for Android | IE mobile | Opera Android | iOS Safari |
|---|---|---|---|---|---|---|---|
Basic Support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

