0

可能重复:
如何在 JavaScript 中将数字格式化为货币?

这是我正在使用的当前代码:

f[0].update(parseFloat($('tier').getValue().replace('$','').replace(',',''))*parseFloat(text.replace('$','').replace(',','')));

我遇到的问题是价格显示没有$正确的货币。例如,应显示为的内容$29.50显示为29.5或应显示为的内容$5.00显示为5

4

1 回答 1

0

看看accounting.jstoFixed()中的方法。这将解决您的舍入错误并正确格式化资金

   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

于 2012-11-09T22:29:52.767 回答