I have created what I believe to be the world’s premier currency formatting function, yes, the last USD Currency Formatter you will ever need. Period.

Today I needed to format strings like ‘1234567’ and ‘1234567.8’ and even ‘1234567.89’ as USD complete with commas and two-digit cents. A quick googling returned a few stackoverflow posts but people are posting up stuff like .NET and whatever. Fine, I’ll do it myself…. Anyway, if you’re searching for the same thing your search ends now. The seas have parted and all you need to do is skip gayly across the sea floor (don’t trip on any mollusks) towards the nirvana that is presented below.

Did you catch the sarcasm? Good. There are lots of ways to approach this. I think mine is pretty simple and simple more often than not works the best. Here it is:

...
function formatAsDollarAmount(num){
   var str = num.toString();
   var strWithCommas = '';
   var finalString = '';
   var dollarsAndCents = str.indexOf('.') != -1 ? str.split('.') : [str,'00'];
   if (dollarsAndCents[0].length > 3){
      var skip = dollarsAndCents[0].length % 3;
      var strForCommas = dollarsAndCents[0].substr(skip,dollarsAndCents[0].length);
      var groupingRegEx = (/(\d){3}/g);
      var groups = strForCommas.match(groupingRegEx);
      for (var i=0;i 0 ? ',' : '') + strWithCommas + '.' + (dollarsAndCents[1].length != 2 ? dollarsAndCents[1] + '0' : dollarsAndCents[1]);
   return finalString;
}
...

Now the purists among you might proclaim “It doesn’t do any rounding!” To which I reply “Do it yourself you maggot! Or do not for I don’t care! Surely what little grey matter you possess would not be taxed too harshly by such a trivial addition!”

But, yes, it doesn’t. How can I claim then that this thing is so grand? Well, the problem is adding the commas and the cents, isn’t it? This does so in a very neat and tidy manner, does it not? Yes, I thought you would agree. So, go ahead and feed it your rounded numbers and then…. begone with you!

Eh? Are you still about? (Sigh), ok then….. I now present to you with much fanfare the new **improved** USD Currency Formatter To End The Previously Proclaimed USD Currency Formatter-ending Formatter™!

This now contains that which you’ve been groveling for: comma insertion, 2-digit cents and rounding to the nearest hundredth. Take it and prosper.

...
function formatAsDollarAmount(num){
   num = Math.round(num * 100) / 100; // this is the part your brain was having problems with
   var str = num.toString();
   var strWithCommas = '';
   var finalString = '';
   var dollarsAndCents = str.indexOf('.') != -1 ? str.split('.') : [str,'00'];
   if (dollarsAndCents[0].length > 3){
      var skip = dollarsAndCents[0].length % 3;
      var strForCommas = dollarsAndCents[0].substr(skip,dollarsAndCents[0].length);
      var groupingRegEx = (/(\d){3}/g);
      var groups = strForCommas.match(groupingRegEx);
      for (var i=0;i 0 ? ',' : '') + strWithCommas + '.' + (dollarsAndCents[1].length != 2 ? dollarsAndCents[1] + '0' : dollarsAndCents[1]);
   return finalString;
}
...

Meh.