Format Decimal - Node.js Number

Node.js examples for Number:Decimal

Description

Format Decimal

Demo Code


Number.prototype.formatDecimal = function() {
  var nStr = this.toFixed(2);
  x = nStr.split('.');
  x1 = x[0];/*  w ww. ja  v a2  s.c  o m*/
  x2 = x.length > 1 ? '.' + x[1] : '';
  var rgx = /(\d+)(\d{3})/;
  while (rgx.test(x1)) {
    x1 = x1.replace(rgx, '$1' + ',' + '$2');
  }
  return x1 + x2;
};

Related Tutorials