数字货币格式化问题

时间:2012-11-09 22:26:03

标签: javascript

  

可能重复:
  How can I format numbers as money in JavaScript?

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

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

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

1 个答案:

答案 0 :(得分:0)

查看accounting.js中的toFixed()方法。这将解决您的舍入误差和也正确格式化钱

   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