首页 > 其他 > 详细

怎样对小数进行向上取整 / 向下取整 / 四舍五入 / 保留n位小数 / 生成随机数

时间:2019-10-16 20:57:28      阅读:327      评论:0      收藏:0      [点我收藏+]

1. 向上取整使用: Math.ceil()

Math.ceil(0.1); // 1
Math.ceil(1.9); // 2

 

2. 向下取整使用: Math.floor()

Math.floor(0.1); // 0
Math.floor(1.9); // 1

 

3. 四舍五入取整使用: Math.round()

Math.round(0.1); // 0
Math.round(1.9); // 2

 

4. 保留n位小数使用: Number().toFixed(n)

Number(2.134).toFixed(2); // 2.13
Number(2.135).toFixed(2); // 2.13
Number(2.136).toFixed(2); // 2.14

 

5. 生成区间随机数使用Math.random();

// 方法1
parseInt(Math.random()*(max-min+1)+min,10);

// 方法2 Math.floor(Math.random()
*(max-min+1)+min);

 

怎样对小数进行向上取整 / 向下取整 / 四舍五入 / 保留n位小数 / 生成随机数

原文:https://www.cnblogs.com/aisowe/p/11687241.html

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