title: Formatting numbers for commas and decimal places with Javascript
body: <p>I'm trying to create a function with javascript that displays all numbers with two decimal points and adds commas every three digits (1,000 10,000 100,000 etc).  </p>
  
  <p>At first I had this:  </p>
  
  <pre><code>var formatNumber = function(num) {
      return parseFloat((num).toFixed(2)).toLocaleString();
  };
  </code></pre>
  
  <p>This works very well but with one exception.</p>
  
  <p>1.2 != 1.20 in the final output.  It just shows up as 1.2</p>
  
  <p>But everything else is good.  124.5879697 = 124.59, 10000 = 10,000, and 10586.357 = 10,586.36</p>
  
  <p>The issue is the final output is going to display as money, so displaying 10000 as $10,000 is fine.  But displaying 1.2 as $1.2 looks a little off.</p>
  
  <p>To get around this I tried to add the following modification: </p>
  
  <pre><code>var formatNumber = function(num) {
     return parseFloat((Math.round(num*100)/100)).toFixed(2).toLocaleString();
  };
  </code></pre>
  
  <p>This carries everything out to two decimal places, which is fine, but it seems to have de-activated toLocaleString because now nothing displays commas.  </p>
  
  <p>I'm looking to use pure Javascript with this and not jQuery.  It seems there are a lot of js questions on this topic about one or the other issue, but not combining both.</p>
  
  <p>Thank you.</p>