首页 > Web开发 > 详细

JS保留小数 去尾法 进一法 四舍五入法

时间:2016-03-10 12:22:57      阅读:1569      评论:0      收藏:0      [点我收藏+]

toFixed 四舍五入遇到坑。

1.235.toFixed(2) = 1.23
1.2350001.toFixed(2) = 1.24

 

//去尾法
Number.prototype.toFloor = function (num) {
return Math.floor(this * Math.pow(10, num)) / Math.pow(10, num);
};

//进一法
Number.prototype.toCeil = function (num) {
return Math.ceil(this * Math.pow(10, num)) / Math.pow(10, num);
};

//四舍五入法
Number.prototype.toRound = function (num) {
return Math.round(this * Math.pow(10, num)) / Math.pow(10, num);
};

JS保留小数 去尾法 进一法 四舍五入法

原文:http://www.cnblogs.com/nbgzc/p/5261015.html

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