0

Possible Duplicate:
How can I format numbers as money in JavaScript?

This is the current code I am using:

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

The problem I am having is that the price shows without the $ and not proper currency. For example, something that should show as $29.50 shows as 29.5 or something that should show as $5.00 shows as 5.

Community
  • 1
  • 1
ryanb4614
  • 153
  • 3
  • 16

1 Answers1

0

Have a look at toFixed() method from accounting.js. This will fix your rounding errors & also format the money correctly

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

Link: http://josscrowcroft.github.com/accounting.js/#methods

CuriousMind
  • 33,537
  • 28
  • 98
  • 137
  • This is for magento so I don't believe the accounting.js would benefit I think the code needs to be changed a little. – ryanb4614 Nov 09 '12 at 22:41
  • Its not like you can't run JS on magento powered sites. Anyways, if for some reason you can't use the complete library. you can just take the code you need(code snippet posted here) & use that to write your own function. would be just 4-5lines of code – CuriousMind Nov 09 '12 at 22:47