javascript Math對象

來源:互聯網
上載者:User

標籤:隨機數   round   java   color   對象   最大值   app   values   算數   

Math(算數對象)執行常見的算數任務。

1、Math對象的屬性

Math.E //自然對數的底數,即常量e的值Math.LN10 //10的自然對數Math.LN2 //2的自然對數Math.LOG2E //已2為底e的對數Math.LOG10E //已10為底e的對數Math.PI //π的值Math.SQRT1_2 //1/2的平方根(即2的平方根的倒數)Math.SQRT2 //2的平方根

2、min()、max()和apply()方法

  用於確定一組數值中的最小和最大值。

例:

var max = Math.max(1, 2, 3, 4, 5);//5var min = Math.min(1, 2, 3, 4, 5);//1var values = [1, 2, 3, 4, 5];var max = Math.max.apply(Math, values);//5var min = Math.min.apply(Math, values);//1

3、舍入方法

(1)Math.ceil()執行向上舍入。

(2)Math.floor()執行向下舍入。

(3)Math.round()執行標準舍入,即四捨五入。

例:

console.log(Math.ceil(2.1));//3console.log(Math.floor(3.9));//3console.log(Math.round(3.3));//3

4、random()方法

  返回大於等於0小於1的一個隨機數。

例:

console.log(Math.floor(Math.random() * 10 + 1));//1到10的值

5、其他方法

Math.abs(num);//返回num的絕對值Math.exp(num);//返回Math.E的num次冪Math.log(num);//返回num的自然對數Math.pow(num, power);//返回num的power次冪Math.sqrt(num);//返回num的平方根Math.acos(x);//返回x的反餘弦值...

 

javascript Math對象

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.