Math.round()、Math.ceil()、Math.floor()与Math.random()的区别?

Math.round(x)  四舍五入 加上0.5向下取整

  Math.round(1.5)  2

  Math.round(-11.5)  -11

  Math.round(-11.2)  -10

Math.ceil(x)  不小于x的最小整数

  Math.ceil(1.5)  2

  Math.ceil(-1.5)  -1

Math.floor(x)  返回小于等于x的最大整数

  Math.floor(5.99))   5
  Math.floor(-5.99)  -6

Math.random()  生成0和1之间的随机小数

  Math.random() * 7 生成0和7之间的随机小数
  Math.random() * 7 + 1生成0和8之间的随机小数
  Math.floor(Math.random() * 7 + 1)生成0和8之间的随机整数,注意是整数,不是小数

原文地址:https://www.cnblogs.com/jiechn/p/3847803.html