title: Javascript Number formatting min/max decimals
body: <p>I'm trying to create a function that can format a number with the minimum decimal places of 2 and a maximum of 4. So basically if I pass in 354545.33 I would get back 354,545.33 and if I pass in 54433.6559943 I would get back 54,433.6559. </p>
  
  <pre><code>function numberFormat(num){
      num = num+"";
      if (num.length &gt; 0){
  
          num = num.toString().replace(/\$|\,/g,'');
          num = Math.floor(num * 10000) / 10000;
  
          num += '';
          x = num.split('.');
          x1 = x[0];
          x2 = x.length &gt; 1 ? '.' + x[1] : '';
          var rgx = /(\d+)(\d{3})/;
          while (rgx.test(x1)) {
              x1 = x1.replace(rgx, '$1' + ',' + '$2');
          }
          return x1 + x2;
  
      }
      else{
          return num;
      }
  }
  </code></pre>