JS常用的Math对象

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)); //6
 console.log(Math.floor(5.99)); //5

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

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

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

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

例:0-5之间的随机整数

console.log(Math.floor(Math.random() * (5 + 1))) //0-5之间的随机整数

公式:min+Math.floor(Math.random()*(Max-min+1))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值