Math

作者: 任仪凡 | 来源:发表于2018-07-24 20:28 被阅读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);

相关文章

网友评论

      本文标题:Math

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