欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js中常用的Math方法總結(jié)

 更新時間:2017年01月12日 14:04:42   作者:風雨后見彩虹  
本文主要介紹了js中常用的Math方法,具有一定的參考價值,下面跟著小編一起來看下吧

1.min()和max()方法

Math.min()用于確定一組數(shù)值中的最小值。Math.max()用于確定一組數(shù)值中的最大值。

alert(Math.min(2,4,3,6,3,8,0,1,3));              //最小值
alert(Math.max(4,7,8,3,1,9,6,0,3,2));         //最大值

2.舍入方法

Math.ceil()執(zhí)行向上舍入,即它總是將數(shù)值向上舍入為最接近的整數(shù);

Math.floor()執(zhí)行向下舍入,即它總是將數(shù)值向下舍入為最接近的整數(shù);

Math.round()執(zhí)行標準舍入,即它總是將數(shù)值四舍五入為最接近的整數(shù);

例如:

alert(Math.ceil(25.9));                   //26
alert(Math.ceil(25.5));                   //26
alert(Math.ceil(25.1));                   //26
alert(Math.floor(25.9));                  //25
alert(Math.floor(25.5));                  //25
alert(Math.floor(25.1));                  //25
alert(Math.round(25.9));                     //26
alert(Math.round(25.5));                     //26
alert(Math.round(25.1));                     //25

3.random()方法

Math.random()方法返回介于0到1之間一個隨機數(shù),不包括0和1。如果想大于這個范圍的話,可以套用一下公式:

值 = Math.floor(Math.random() * 總數(shù) + 第一個值)

例如:

alert(Math.floor(Math.random() * 10 + 1));        //隨機產(chǎn)生1-10之間的任意數(shù)

for (var i = 0; i<10;i ++) {
    document.write(Math.floor(Math.random() * 10 + 5));       //5-14之間的任意數(shù)
    document.write('<br />');
}

為了更加方便的傳遞想要范圍,可以寫成函數(shù):

function selectFrom(lower, upper) {
    var sum = upper - lower + 1;                      //總數(shù)-第一個數(shù)+1
    return Math.floor(Math.random() * sum + lower);
}
for (var i=0 ;i<10;i++) {
    document.write(selectFrom(5,10));                 //直接傳遞范圍即可
    document.write('<br />');
}

4.其它方法

如下表格:

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!

  法

  明

Math.abs(num)

返回num的絕對值

Math.exp(num)

返回Math.E的num次冪

Math.log(num)

返回num的自然對數(shù)

Math.pow(num,power)

返回num的power次冪

Math.sqrt(num)

返回num的平方根

Math.acos(x)

返回x的反余弦值

Math.asin(x)

返回x的反正弦值

Math.atan(x)

返回x的反正切值

Math.atan2(y,x)

返回y/x的反正切值

Math.cos(x)

返回x的余弦值

Math.sin(x)

返回x的正弦值

Math.tan(x)

返回x的正切值

相關(guān)文章

最新評論