Math對象:math對象用于執行數學任務。
注意:Math 對象并不像 Date 和 String 那樣是對象的類,因此沒有構造函數 Math(),像 Math.sin() 這樣的函數只是函數,不是某個對象的方法。您無需創建它,通過把 Math 作為對象使用就可以調用其所有屬性和方法。
Math的內置方法:
- math.ceil(x) 可以對x進行向上取整
dacument.write(math.ceil(7.9)
//結果:8
dacument.write(math.ceil(-7.9)
//結果:-7
- math.floor() 可以對數進行向下取整
dacument.write(math.floor(7.9)
//結果:7
dacument.write(math.floor(-7.9)
//結果:-8
- math.max(x,y)返回其中的最大值
dacument.write(math.max(7,9))
//結果:9
dacument.write(math.max(-7,-9)
//結果:-7
- math.min() 返回其中最小值
//結果:7
dacument.write(math.min(7,-9))
//結果:-9
- Math.random() 隨機生成0~1之間的隨機數
dacument.write(math.random()*10)
//隨機生成0-10之間的數
//生成X-y之間的數
X+document.write(math.random()*(y-x))
- Math.round(x) 把x四舍五入為最近的整數
dacument.write(math.round(4.5))
//結果:5
- math.pow(x,y) 返回x的y次冪
dacument.write(math.pow(2,2))
//結果:4
- Math.abs() 返回數的絕對值
dacument.write(math.abs(-8))
//結果:8