美文网首页
2018-08-06

2018-08-06

作者: 哇塞薛之谦 | 来源:发表于2018-08-10 17:22 被阅读0次

1.Math.ceil() 上取整

alert(Math.ceil(3.5)) //4
alert(Math.ceil(3.4)) //4
alert(Math.ceil(-3.5)) //-3
alert(Math.ceil('3.1')) //4
alert(Math.ceil('3.1')) //number 会做隐式类型转换
2.Math.floor() 下取整

alert(Math.floor(3.5)) //3
alert(Math.floor(3.4)) //3
alert(Math.floor(-3.5)) //-3
3.Math.random(); 产生一个0和1之间的随机数(含0不含1),也可以自己填数

//返回一个0和1之间的随机数
alert(Math.random());
//返回一个0和100之间的随机数
alert(Math.random(100));
//返回一个0和100之间的随机整数
alert(Math.floor(Math.random(100)));
//返回一个100到200之间的随机数
alert(Math.random()*(200-100)+100);

相关文章

网友评论

      本文标题:2018-08-06

      本文链接:https://www.haomeiwen.com/subject/uxjlbftx.html