輕鬆掌握JavaScript中的Math object數學對象_基礎知識

來源:互聯網
上載者:User

對於內建的Math數學常項和函數也有一些屬性和方法。 比方說, Math對象的 PI 屬性會有屬性值 pi (3.141...),你可以像這樣調用它:

Math.PI

同理,標準數學函數也是Math的方法。 這些包括三角函數​​,對數,指數,和其他函數。比方說你想使用三角函數 sin, 你可以這麼寫:

Math.sin(1.56)

需要注意的是Math的所有三角函數參數都是弧度制。
和其他對象不同,你不能夠建立一個自己的Math對象。你只能使用內建的Math對象。

eg:
1.min( )和max(
)

var value = [1,2,3,4,5,6,7,8];var max = Math.max.apply(Math, values);

2.舍入方法
Math.ceil( ):向上舍入
Math.floor( ):向下舍入
Math.round( ):四捨五入

3.random( )
Math.random( )方法返回介於0和1之間的一個隨機數,不包括0和1

var num = Math.floor(Math.random()*10, + 1)//返回1-10之間的數

4.round()
如何使用 round()。

<html><body><script type="text/javascript">document.write(Math.round(0.60) + "<br />")document.write(Math.round(0.50) + "<br />")document.write(Math.round(0.49) + "<br />")document.write(Math.round(-4.40) + "<br />")document.write(Math.round(-4.60))</script></body></html>

5.random()
如何使用 random() 來返回 0 到 1 之間的隨機數。

<html><body><script type="text/javascript">document.write(Math.random())</script></body></html>

聯繫我們

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