js中Math之random,round,ceil,floor用法总结

js的知识点总是边学边忘,这不,时间长不写,Math中这个几个方法又开始混淆了。翻阅文档一个个又查了一遍,赶紧记个笔记,算是巩固一下基础知识吧!文章太基础了,高手直接跳过,谢谢~

1.random
返回介于 0 ~ 1 之间的一个随机数。

1
2
3
4
5
6
var arr=[];
for(var i=0;i<5;i++){
var r=Math.random()*10;
arr.push(r);
}
console.log(arr);//[4.921736601237535, 0.6815536329616378, 9.11504234974638, 2.9633945899038325, 3.7655477568488047]

2.round
把一个数字舍入为最接近的整数。

1
2
3
4
5
6
console.log(Math.round(0.60))//1
console.log(Math.round(0.40))//0
console.log(Math.round(5))//5
console.log(Math.round(5.1))//5
console.log(Math.round(-5.1))//-5
console.log(Math.round(-5.9))//-6

3.ceil
ceil() 方法执行的是向上取整计算,它返回的是大于或等于函数参数,并且与之最接近的整数。

1
2
3
4
5
6
console.log(Math.ceil(0.60))//1
console.log(Math.ceil(0.40))//1
console.log(Math.ceil(5))//5
console.log(Math.ceil(5.1))//6
console.log(Math.ceil(-5.1))//-5
console.log(Math.ceil(-5.9))//-5

4.floor
floor() 方法执行的是向下取整计算,它返回的是小于或等于函数参数,并且与之最接近的整数。

1
2
3
4
5
6
console.log(Math.floor(0.60))//0
console.log(Math.floor(0.40))//0
console.log(Math.floor(5))//5
console.log(Math.floor(5.1))//5
console.log(Math.floor(-5.1))//-6
console.log(Math.floor(-5.9))//-6

当然,Math中的方法非常多,可是我又不想接着写了,先这样吧~

参考链接:
w3school:http://www.w3school.com.cn/jsref/jsref_obj_math.asp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值