《菜鳥教程》-TypeScript Number

TypeScript 與 JavaScript 類似,支持 Number 對象。
Number 對象是原始數值的包裝對象。
語法:

var num = new Number(value);

注意: 如果一個參數值不能轉換爲一個數字將返回 NaN (非數字值)。

Number 對象屬性

MAX_VALUE: 可表示的最大的數,MAX_VALUE 屬性值接近於 1.79E+308。大於 MAX_VALUE 的值代表 "Infinity"。
MIN_VALUE:可表示的最小的數,即最接近 0 的正數 (實際上不會變成 0)。最大的負數是 -MIN_VALUE,MIN_VALUE 的值約爲 5e-324。小於 MIN_VALUE ("underflow values") 的值將會轉換爲 0。
NaN:非數字值(Not-A-Number)。
NEGATIVE_INFINITY:負無窮大,溢出時返回該值。該值小於 MIN_VALUE。
POSITIVE_INFINITY:正無窮大,溢出時返回該值。該值大於 MAX_VALUE。
代碼示例:

console.log(Number.MAX_VALUE); // 1.7976931348623157e+308
console.log(Number.MIN_VALUE); // 5e-324
console.log(Number.NaN); // NAN
console.log(Number.NEGATIVE_INFINITY); // -Infinity
console.log(Number.POSITIVE_INFINITY); // Infinity

Number 對象方法

toExponential():把對象的值轉換爲指數計數法。
toFixed():把數字轉換爲字符串,並對小數點指定位數。
toLocaleString():把數字轉換爲字符串,使用本地數字格式順序。
toPrecision():把數字格式化爲指定的長度。
toString():把數字轉換爲字符串,使用指定的基數。數字的基數是 2 ~ 36 之間的整數。若省略該參數,則使用基數 10。
valueOf():返回一個 Number 對象的原始數字值。

代碼示例:

let num = new Number(12345678910.12345);
console.log(num.toExponential()); // 1.234567891012345e+10
console.log(num.toFixed(3)); // 12345678910.123
console.log(num.toLocaleString()); // 12,345,678,910.123
console.log(num.toPrecision(3)); // 1.23e+10
console.log(num.toString()); // 12345678910.12345
console.log(num.valueOf()); // 12345678910.12345

附錄(TS3.9.7接口部分源碼):

interface Number {
    /**
     * Returns a string representation of an object.
     * @param radix Specifies a radix for converting numeric values to strings. This value is only used for numbers.
     */
    toString(radix?: number): string;

    /**
     * Returns a string representing a number in fixed-point notation.
     * @param fractionDigits Number of digits after the decimal point. Must be in the range 0 - 20, inclusive.
     */
    toFixed(fractionDigits?: number): string;

    /**
     * Returns a string containing a number represented in exponential notation.
     * @param fractionDigits Number of digits after the decimal point. Must be in the range 0 - 20, inclusive.
     */
    toExponential(fractionDigits?: number): string;

    /**
     * Returns a string containing a number represented either in exponential or fixed-point notation with a specified number of digits.
     * @param precision Number of significant digits. Must be in the range 1 - 21, inclusive.
     */
    toPrecision(precision?: number): string;

    /** Returns the primitive value of the specified object. */
    valueOf(): number;
}

interface NumberConstructor {
    new(value?: any): Number;
    (value?: any): number;
    readonly prototype: Number;

    /** The largest number that can be represented in JavaScript. Equal to approximately 1.79E+308. */
    readonly MAX_VALUE: number;

    /** The closest number to zero that can be represented in JavaScript. Equal to approximately 5.00E-324. */
    readonly MIN_VALUE: number;

    /**
     * A value that is not a number.
     * In equality comparisons, NaN does not equal any value, including itself. To test whether a value is equivalent to NaN, use the isNaN function.
     */
    readonly NaN: number;

    /**
     * A value that is less than the largest negative number that can be represented in JavaScript.
     * JavaScript displays NEGATIVE_INFINITY values as -infinity.
     */
    readonly NEGATIVE_INFINITY: number;

    /**
     * A value greater than the largest number that can be represented in JavaScript.
     * JavaScript displays POSITIVE_INFINITY values as infinity.
     */
    readonly POSITIVE_INFINITY: number;
}

/** An object that represents a number of any kind. All JavaScript numbers are 64-bit floating-point numbers. */
declare var Number: NumberConstructor;
如果覺得文章寫得還行,請點個贊。如果想與我進一步交流,可以關注我的公衆號或者加我的微信。

微信號:zhanglanjiahuo
公衆號:前端微說

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章