1. 程式人生 > >ES6 之 Math對象的擴展

ES6 之 Math對象的擴展

math UNC 3.5 www run style ref 平方和 ron

1、ES5

http://www.w3school.com.cn/jsref/jsref_obj_math.asp

2、ES6

Math.trunc() - 取整,去掉一個數的小數部分

    console.log(Math.trunc(3.5)); // 3
    console.log(Math.trunc(-3.5)); // -3
    // 對於非數值,Math.trunc() 內部使用Number()方法鹹江奇轉化為數值
    console.log(Math.trunc(‘123.456‘)); //123
    console.log(Math.trunc(NaN)); // NaN
    console.log(Math.trunc(‘abc‘)); //
NaN console.log(Math.trunc()); // NaN

Math.sign()

    // Math.sign() // 用來判斷一個數是蒸熟還是負數 0,對於非數值,先轉化,後判斷
    /*
    * 參數為正數 返回+1
    * 參數為負數,返回-1
    * 參數為0,返回0
    * 參數為-0,返回-0
    * 參數為NaN,返回NaN
    * */

Math.cbrt()

// 計算立方根
    console.log(Math.cbrt(-8)); // -2

Math.hypot()

// 返回所有參數的平方和的平凡根
    console.log(Math.hypot(3,4,5)); //
7.0710678118654755

ES6 之 Math對象的擴展