JS保留小數 去尾法 進一法 四舍五入法


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);
};


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM