例取一个5位数的随机数
Math.round(Math.random()*100000)=66666
1.Math.random():得到0~1之间的随机数
2.Math.round():四舍五入
例:
小数点后第一位<5
正数:Math.round(10.46)=10
负数:Math.round(-10.46)=-10
小数点后第一位>5
正数:Math.round(10.68)=11
负数:Math.round(-10.68)=-11
小数点后第一位=5
正数:Math.round(10.5)=11
负数:Math.round(-10.5)=-10
总结:(小数点后第一位)大于五全部加,等于五正数加,小于五全不加。
3.Math.ceil():向上取整
根据“ceil”的字面意思“天花板”去理解;
例如:
Math.ceil(10.46)=Math.ceil(10.68)=Math.ceil(10.5)=11
Math.ceil(-10.46)=Math.ceil(-10.68)=Math.ceil(-10.5)=-10
4.Math.floor():向下取整
根据“floor”的字面意思“地板”去理解;
例如:
Math.ceil(10.46)=Math.ceil(10.68)=Math.ceil(10.5)=10
Math.ceil(-10.46)=Math.ceil(-10.68)=Math.ceil(-10.5)=-11