輕松掌握JavaScript中的Math object數(shù)學對象
來源:易賢網(wǎng) 閱讀:807 次 日期:2016-06-20 17:05:49
溫馨提示:易賢網(wǎng)小編為您整理了“輕松掌握JavaScript中的Math object數(shù)學對象”,方便廣大網(wǎng)友查閱!

Math對象或者說是類型旗下?lián)碛泻芏嗥綍r常用的數(shù)學函數(shù),雖然并不像Date和String那樣擁有類似于類和方法那樣的使用方式,不過并不妨礙您通過本文來輕松掌握JavaScript中的Math object數(shù)學對象^^

對于內(nèi)置的Math數(shù)學常項和函數(shù)也有一些屬性和方法。 比方說, Math對象的 PI 屬性會有屬性值 pi (3.141...),你可以像這樣調(diào)用它:

Math.PI

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

Math.sin(1.56)

需要注意的是Math的所有三角函數(shù)參數(shù)都是弧度制。

和其他對象不同,你不能夠創(chuàng)建一個自己的Math對象。你只能使用內(nèi)置的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之間的一個隨機數(shù),不包括0和1

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

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 之間的隨機數(shù)。

<html>

<body>

<script type="text/javascript">

document.write(Math.random())

</script>

</body>

</html>

更多信息請查看網(wǎng)絡編程
易賢網(wǎng)手機網(wǎng)站地址:輕松掌握JavaScript中的Math object數(shù)學對象

2026國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權所有:易賢網(wǎng)