《JavaScript進階程式設計》閱讀筆記(十二):內建對象Math

來源:互聯網
上載者:User
Math對象的屬性

E:值e,自然對數的底

LN10:10的自然對數

LN2:2的自然對數

LOG2E:以2為底E的對數

LOG10E:以10為底E的對數

PI:值派

SQRT1_2:1/2 的平方根

SQRT2:2的平方根

Math對象的方法:最大值與最小值

min()&&max()用於取一組數中的最小值跟最大值。

樣本:

var iMax=Math.Max(1,2,3);
alert(iMax);//outputs 3
var iMin=Math.Min(1,2,3);
alert(iMin);//outputs 1
約對值

abs()用於返回數位絕對值。

樣本:

var iNegOne=Math.abs(-1);
alert(iNegOne);//oupputs 1
var iPosOne=Math.abs(1);
alert(iPosOne);//outputs 1
把小數舍入成整數

ceil()為向上舍入函數,總是把數字向上舍入到最接近的值

floor()為向下舍入函數,總是把數字向下舍入到最接近的值

round()為四捨五入法取整

樣本:

alert(Math.ceil(25.5));//oputpus 26
alert(Math.floor(25.5));//oputpus 25
alert(Math.round(25.5));//oputpus 26
指數計算

exp()用於把Math.E升到指定的冪

log()用於返回特定數位自然對數

pow()用於把指定的數字升到指定的冪

sqrt()用於返回指定數的平方根

三角函數方法

acos(x)用於返回x的反餘弦值

asin(x)用於返回x的反正弦值

atan(x)用於返回x的反正切值

atan2(y,x)用於返回y/x的反餘弦值

cos(x)用於返回x的餘弦值

sin(x)用於返回x的正弦值

tan(x)用於返回x的正切值

隨機數函數

random()用於返回一個0到1間的隨機數,包括0但不包括1

在某個範圍內選擇隨機數:

function selectFrom(iFirstValue,iLastValue){
  var iChoices=iLastValue-iFirstValue+1;
  return Math.floor(Math.random()*iChoices+iFirstValue);
}

//demo
var iNum=selectFrom(2,10);
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.