While working on Slikcalc, I was trying to find the easiest way to format a number for currency. I had found some implementations that were pretty complex, using regex and absolute values, and thought there had to be a simpler way. This is what I came up with and so it works great in all the browsers I’ve tested (IE 6/7, Firefox 2 (windows & mac), Opera 9 (windows and mac).
function formatCurrency(num) {
num = isNaN(num) || num === '' || num === null ? 0.00 : num;
return parseFloat(num).toFixed(2);
}
Just thought I’d share for anyone working with something similar.


9 Comments
Thanks for this option and can you please provide a working sample. Specifically, if i already have code in place, how can i run this function against a variable and/or form field value? I would introduce my code, but I don’t want you to fall out of your chair laughing or anything!!
Just learning.
Thanks and sorry if its a ridiculous question!
I would imagine you want this code to be used in conjunction with an input field, perhaps onblur you want to format the currency for that field. You could set that up a number of ways, preferably using some type of javascript library to add the event listener. For the sake of simplicity, I’ll assume no library is being used, and you just want to add it inline. You would need to have the formatCurrency function defined somewhere, such as below (I’ve wrapped it into a util object for an example of how you would avoid global functions):
var util = {
formatCurrency : function(num) {
num = isNaN(num) || num === '' || num === null ? 0.00 : num;
return parseFloat(num).toFixed(2);
}
}
You could then add the onblur listener inline in your html. I would suggest using a library like YUI or jQuery to handle adding your listeners, but without getting into that, here is what you would have inline for your input you wanted to have formatted on blur of the field:
<input type="text" name="somefield" onblur="this.value = util.formatCurrency(this.value);" />
This code warrants a disclaimer, as I haven’t tested it, but it should give you a good start. Hope that helps!
Where IS the comma?
$ 1,000.00
The COMMA, also MINUS and DOLLAR sign, and a few more details….
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g, ”);
if (isNaN(num)) num = “0″;
sign = (num == (num = Math.abs(num)));
num = Math.floor(num * 100 + 0.50000000001);
cents = num % 100;
num = Math.floor(num / 100).toString();
if (cents < 10) cents = “0″ + cents;
for (var i = 0; i < Math.floor((num.length – (1 + i)) / 3); i++)
num = num.substring(0, num.length – (4 * i + 3)) + ‘,’ + num.substring(num.length – (4 * i + 3));
return (((sign) ? ” : ‘-’) + ‘$’ + num + ‘.’ + cents);
}
I was able to use your JS currency function in a project at work. Your work is interesting. Thanks for sharing
Hope I may be able to reciprocate somehow, someday. Nice to have the option to use tags in the comments section.
Thanks Mate. You are genius
Thank you, very well. buy nolvadex low cost http://www.stumbleupon.com/stumbler/med-brother/ buy nolvadex drug Many thanks.
I’m glad to see you! buy clenbuterol nolvadex online buy nolvadex liquid
buy clenbuterol nolvadex online Thank you very much.
I am wondering if anyone can tell me where the function submitted by Alex G aboce would be included?
Nice. Thanks
2 Trackbacks
[...] new version of Slikcalc (1.0) was released. This release simplified some code, mainly the slikcalc.formatCurrency() method. There was also a bug where a calculator could be initialized more than once depending on timing [...]
Ritalin its side effects….
Ritalin. Ritalin as an appetite stimulant….