JS中Math函式的常用方法

2021-10-14 14:37:05 字數 942 閱讀 1692

math是數學函式,但又屬於物件資料型別typeof math=> 『object』

console.dir(math)檢視math的所有函式方法。

1,math.abs()獲取絕對值

math.abs(-12) = 12
2,math.ceil() and math.floor()向上取整和向下取整

3,math.round()四捨五入

注意:正數時,包含5是向上取整,負數時包含5是向下取整。

math.round(-16.3) = -12

math.round(-16.5) = -12

math.round(-16.51) = -13

4,math.random()取[0,1)的隨機小數

案例1:獲取[0,10]的隨機整數

math.round(math.random()*10)
案例2:獲取[n,m]之間的隨機整數

math.round(math.random()*(m-n)+n)
5,math.max() and max.min()獲取一組資料中的最大值和最小值

6,math.pi獲取圓周率π 的值

7,math.pow() and math.sqrt()

math.pow()獲取乙個值的多少次冪

math.sqrt()對數值開方

math.pow(10,2) = 100;

math.sqrt(100) = 10;

js中math函式的常用方法

math.abs j絕對值 math.ceil 整數 向上取整和向下取整 console.log math.ceil 12.95 13 console.log math.floor 12.3 12 math.round 四捨五入 注意 正數時,包含5是向上取整,負數時包含5是向下取整。math.ra...

JS中Math函式的常用方法

console.dir math 可檢視math的所有函式方法 math.abs 獲取絕對值 math.abs 12 12math.ceil and math.floor 向上取整和向下取整 console.log math.ceil 12.03 13 console.log math.ceil 1...

JS中Math函式的常用方法

math是數學函式,但又屬於物件資料型別typeof math object console.dir math 檢視math的所有函式方法。1,math.abs 獲取絕對值 math.abs 12 12 2,math.ceil and math.floor 向上取整和向下取整 console.log...