方法一:寫一個簡單的了函數
//四舍五入函數
var round = function (num, digits) {
return Math.round(num * Math.pow(10, digits)) / Math.pow(10, digits);
};
round(0.455, 2); // 結果:0.46
方法二:為Number增加一個round方法
//為Number類型增加round方法
delete Number.prototype.round;
Number.prototype.round=function (digits) {
var num = this;
return Math.round(num * Math.pow(10, digits)) / Math.pow(10, digits);
};
0.455.round(2); //結果:0.46