Format Currency in Javascript (simplified)

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.

  • del.icio.us
  • Digg
This entry was posted in javascript, slikcalc. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

9 Comments

  1. Posted January 27, 2009 at 5:10 pm | Permalink

    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!

  2. brad
    Posted January 28, 2009 at 8:52 am | Permalink

    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!

  3. Posted February 9, 2009 at 12:51 pm | Permalink

    Where IS the comma?
    $ 1,000.00

  4. Alex G
    Posted May 1, 2009 at 10:09 am | Permalink

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

  5. Joyce
    Posted September 10, 2009 at 6:13 am | Permalink

    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.

  6. Posted October 5, 2009 at 6:39 pm | Permalink

    Thanks Mate. You are genius

  7. Posted November 9, 2009 at 7:25 pm | Permalink

    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.

  8. Wes Davis
    Posted November 21, 2009 at 5:00 pm | Permalink

    I am wondering if anyone can tell me where the function submitted by Alex G aboce would be included?

  9. Alfabravo
    Posted November 23, 2009 at 2:04 pm | Permalink

    Nice. Thanks

2 Trackbacks

  1. [...] 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 [...]

  2. By Ritalin adult depression. on November 15, 2009 at 2:53 pm

    Ritalin its side effects….

    Ritalin. Ritalin as an appetite stimulant….

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>