• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • new 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
    

    上篇:Number

    下篇:Number.NaN