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