首页 > 其他 > 详细

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

时间:2014-07-16 17:40:25      阅读:354      评论:0      收藏:0      [点我收藏+]

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之间的随机整数,注意是整数,不是小数

Math.round()、Math.ceil()、Math.floor()与Math.random()的区别?,布布扣,bubuko.com

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

原文:http://www.cnblogs.com/jiechn/p/3847803.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!