Math是数学函数,但又属于对象数据类型typeof Math=>‘object’
console.dir(Math)查看Math的所有函数方法。
1,Math.abs()获取绝对值
Math.abs(-12) = 12
2,Math.ceil() and Math.floor()向上取整和向下取整
console.log(Math.ceil(12.03))//13
console.log(Math.ceil(12.92))//13
console.log(Math.floor(12.3))//12
console.log(Math.floor(12.9))//12
3,Math.round()四舍五入
注意:正数时,包含5是向上取整,负数时包含5是向下取整。
1、Math.round(-16.3) = -16
2、Math.round(-16.5) = -16
3、Math.round(-16.51) = -17
4,Math.random()取[0,1)的随机小数
案例1:获取[0,10]的随机整数
console.log(parseInt(Math.random()*10))//未包含10
console.log(parseInt(Math.random()*10+1))//包含10
案例2:获取[n,m]之间的随机整数
Math.round(Math.random()*(m-n)+n)
5,Math.max() and Max.min()获取一组数据中的最大值和最小值
console.log(Math.max(10,1,9,100,200,45,78))
console.log(Math.min(10,1,9,100,200,45,78))
6,Math.PI获取圆周率π 的值
console.log(Math.PI)
7,Math.pow() and Math.sqrt()
Math.pow()获取一个值的多少次幂
Math.sqrt()对数值开方
Math.pow(10,2) = 100
Math.sqrt(100) = 10
1.丢弃小数部分,保留整数部分parseInt(5/2)
2.向上取整,有小数就整数部分加1
Math.ceil(5/2)
3,四舍五入.
Math.round(5/2)
4,向下取整
Math.floor(5/2)
Math
对象方法
FF:
Firefox,
IE:
Internet
Explorer
方法
描述
FF
IE
abs(x)
返回数的绝对值。
1
3
acos(x)
返回数的反余弦值。
1
3
asin(x)
返回数的反正弦值。
1
3
atan(x)
以介于
-PI/2
与
PI/2
弧度之间的数值来返回
x
的反正切值。
1
3
atan2(y,x)
返回从
x
轴到点
(x,y)
的角度(介于
-PI/2
与
PI/2
弧度之间)。
1
3
ceil(x)
对数进行上舍入。
1
3
cos(x)
返回数的余弦。
1
3
exp(x)
返回
e
的指数。
1
3
floor(x)
对数进行下舍入。
1
3
log(x)
返回数的自然对数(底为e)。
1
3
max(x,y)
返回
x
和
y
中的最高值。
1
3
min(x,y)
返回
x
和
y
中的最低值。
1
3
pow(x,y)
返回
x
的
y
次幂。
1
3
random()
返回
0
~
1
之间的随机数。
1
3
round(x)
把数四舍五入为最接近的整数。
1
3
sin(x)
返回数的正弦。
1
3
sqrt(x)
返回数的平方根。
1
3
tan(x)
返回角的正切。
1
3
toSource()
返回该对象的源代码。
1
-
valueOf()
返回
Math
对象的原始值。
1
4