溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

JavaScript中常用的Math方法

發(fā)布時間:2020-07-21 09:27:04 來源:網(wǎng)絡(luò) 閱讀:260 作者:00709 欄目:web開發(fā)

1.Math.abs()

作用:用來返回參數(shù)的絕對值

 Math.abs(-3);                       //輸出3
 Math.abs(3);                        //輸出3

2.Math.Max()

作用:返回參數(shù)中最大的那個值
Math.min()
作用:返回參數(shù)中最小的那個值

Math.max(2,-10,45)                //輸出45

假若傳的是一個數(shù)組,那需要借助apply來得到數(shù)組中的最大值或者最小值

var arr=[23,-9,67,34,-89,0];
Math.min.apply(null,arr);                               //輸出-89

3.Math.floor()

作用:返回小于參數(shù)值的最大整數(shù)

 Math.floor(3.2)                                    //輸出3
 Math.floor(-3.2)                                     //輸出-4

4.Math.ceil()

作用:返回大于參數(shù)值的最小整數(shù)

Math.ceil(3.2)          //輸出4
Math.ceil(-3.2)                      //輸出-3

5.Math.round()

作用:四舍五入

 Math.round(0.1)                  //輸出0
 Math.round(0.6)                    //輸出1      

Math.round(-1.1)                  //輸出-1  
Math.round(-1.5)                       //輸出-1  
Math.round(-1.6)                     //輸出-2  

6.Math.pow()

作用:返回以第一個參數(shù)為底數(shù),第二個參數(shù)為冪的指數(shù)值

Math.pow(2,2)                       //輸出4
Math.pow(3,4)          //輸出81

7.Math.sqrt()

作用:返回參數(shù)值的平方根,如果參數(shù)是一個負值,則返回NaN

 Math.sqrt(4)                         //輸出2
 Math.sqrt(-81)                     //輸出NaN
向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI