当前位置: 代码迷 >> 综合 >> JS常用的Math对象
  详细解决方案

JS常用的Math对象

热度:49   发布时间:2024-03-06 12:10:09.0

1.Math.min()/Math.max() (最小值和最大值)

console.log(Math.min(1, 2, 3, 4, 5)); //1
console.log(Math.max(1, 2, 3, 4, 5)); //5

若希望是以数组的方式传入的话可以使用apply()方法

 console.log(Math.max.apply(null, [1, 2, 3, 4, 5])); //返回5

2.Math.ceil()/Math.floor() (向上取整和向下取整)

 console.log(Math.ceil(5.01)); //6console.log(Math.floor(5.99)); //5

3.Math.round() (四舍五入)

 console.log(Math.round(5.5)); //6console.log(Math.round(5.4)); //5

4.Math.random() (0-1之间的随机数)

 console.log(Math.random()) // 0<= 返回值 <1

 

  相关解决方案