title: How to format number to custom string using regexp or function?
body: <p>I need to convert a number to output string format. I need to set a number of decimals, decimal separator, and thousand separator.</p>
  
  <p>I already have a function like this:</p>
  
  <pre><code>function (number, decimals, decPoint, thousandsSep) {
      var i, j, kw, kd, km;
      if (isNaN(decimals = Math.abs(decimals))) {
          decimals = 2;
      }
      if (decPoint == undefined) {
          decPoint = ",";
      }
      if (thousandsSep == undefined) {
          thousandsSep = "";
      }
      var sign = number  3) {
          j = j % 3;
      } else {
          j = 0;
      }
      km = (j ? i.substr(0, j) + thousandsSep : "");
      kw = i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousandsSep);
      kd = (decimals ? dec_point + Math.abs(number - (sign + i)).toFixed(decimals).replace(/-/, 0).slice(2) : "");
      return sign + km + kw + kd;
  }
  </code></pre>
  
  <p>But I need to know, is there a faster or simplier way to do this?</p>