js-Math方法汇总

376 阅读1分钟
方法名称 方法解释 方法示例
ceil() 向上取整,有小数就整数部分加1 Math.ceil(2.1)-->3
round() 四舍五入 Math.round(5.3)-->5
floor() 向下取整 Math.floor(5.7)-->5
abs(x) 返回数的绝对值 Math.abs(-1)-->1
max(x,y) 返回 x 和 y 中的最大值 Math.max(1,2)-->2
min(x,y) 返回 x 和 y 中的最小值 Math.min(1,2,3,4)-->1
pow(x,y) 返回 x 的 y 次幂 Math.pow(2,3)-->8
random() 返回 0 ~ 1 之间的随机数 Math.random()-->0~1
sqrt() 返回num的平方根 Math.sqrt(4)-->2