美文网首页
js--Math round() random()

js--Math round() random()

作者: hsqin | 来源:发表于2019-03-09 11:31 被阅读0次

Math方法整理

常用方法: random max min round floor
常见属性: PI

Math参考地址--w3school

  • Math.round() -- 常见的解释是返回四舍五入之后的整数
    正确解释:返回距离number最接近的整数,如果number与前后两个数值的差值是一样,则取最大的那个数!!!! -3.5距离-3和-4都差了0.5,取最大的,所以是-3。对正数来说,是四舍五入.对负数,则是五舍六入!!!
console.log('Math.round-------↓↓↓↓↓↓')
console.log('-2.2 round之后是 ',Math.round(-2.2)); // -2
console.log('-2.8 round之后是 ',Math.round(-2.8)); // -3
console.log('-3.5 round之后是 ',Math.round(-3.5)); // -3,这里要注意!!!!!
console.log('3.5 round之后是 ',Math.round(-3.5)); // 4
round注意负数.png
  • Math.random() -- 返回介于 0(包含) ~ 1(不包含) 之间的一个随机数
//Math.random()*10+1 -- 返回1(包含)到10(包含)之间的随机数
Math.random() = 0.30452571289742103
  • Math.floor(number) Math.ceil(number)

Math.floor(number) --- 返回≤number的最近的那个整数
Math.ceil(number) ---返回≥number的最近的那个整数
ceil --- vt.装天花板
floor --- n.地板,地面

  • Math.pow(x,y) -- 计算 x的y次幂,参数必填
Math.pow(4,3) == 4*4*4 = 64
  • Math.sqrt(x) -- 计算x的平方根,x>=0,必填。x<0返回NaN
Math.sqrt(9) = 3
Math.sqrt(-1) = NaN

相关文章

网友评论

      本文标题:js--Math round() random()

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