看一看toFixed()
方法從accounting.js。這將解決您的舍入誤差&也格式化錢正確
toFixed() - better rounding for floating point numbers
/**
* Implementation of toFixed() that treats floats more like decimals
*
* Fixes binary rounding issues (eg. (0.615).toFixed(2) === "0.61") that present
* problems for accounting- and finance-related software.
*/
var toFixed = lib.toFixed = function(value, precision) {
precision = checkPrecision(precision, lib.settings.number.precision);
var power = Math.pow(10, precision);
// Multiply up by precision, round accurately, then divide and use native toFixed():
return (Math.round(lib.unformat(value) * power)/power).toFixed(precision);
};
鏈接:http://josscrowcroft.github.com/accounting.js/#methods
我倒是建議使用Numeral.js,用於格式化和操作的數字圖書館。還支持貨幣。 http://numeraljs.com – joko13
這是爲magento,雖然... – ryanb4614