Math 數學函數

數學函數: Math是對象數據類型 ,裏邊提供了許多數學函數方法

常用方法:

1、Math.abs  取絕對值 (正數)

Math.abs(12) -> 12

Math.abs(-12) -> 12 

2、Math.ceil 向上取整

Math.ceil(12) -> 12

Math.ceil(12.1) -> 13

Math.ceil(12.9) -> 13

3、Math.floor 向下取整

Math.floor(12) -> 12

Math.floor(12.1) -> 12

Math.floor(12.9) -> 12

4、Math.round 四捨五入

Math.round(12.3) -> 12

Math.round(12.7) -> 13

5、Math.random  獲取 【0,1) 之間的隨機小數,左閉右開 -  包括0,不包括1

(1)獲取1到10 的正數:

// Math.random()*10 獲取0到10 之前的小數,不包含10
// Math.round() 把上邊小數四捨五入 - 即可獲得0到10 之間的整數
Math.round(Math.random()*10)

(2)獲取3到15 之間的整數:

//  找規律: 想獲取 3 到15之間的數,只需要 Math.random()*12得到 1到12,然後
//  加 3 即可
//  規律: Math.random(最大值 - 最小值)得到0到 (最大值-最小值)之間的數,然後加最小值 
//  即可得到 最小值到最大值 之間的整數
Math.round(Math.random()*12 +3)

 ( 3 ) 獲取 min最小值,到max最大值之間的整數

Math.round(Math.random()*(max-min) + min)

6、Math.PI  圓周率

7、Math.pow( ) 獲取一個值的多少次冪 

Math.sqrt( ) 開平方

Math.pow(10, 2)  -> 10的2次方 -> 100

Math.sqrt(100) -> 100開平方 -> 10

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章