0

重複の可能性:
JavaScriptで数値をお金としてフォーマットするにはどうすればよいですか?

これは私が使用している現在のコードです:

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

私が抱えている問題は、価格が$適切な通貨なしで表示されることです。たとえば、$29.50shows asとして表示される29.5もの、またはshowsasとして表示されるものなどです。$5.005

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 に答える