Javascript Number toLocaleString(fixedNum)

Description

Javascript Number toLocaleString(fixedNum)

Number.prototype.toLocaleString = function(fixedNum) {
  var num = this.valueOf(),
    re = /(-?\d+)(\d{3})/;//  w  w w  . j  a va 2  s .  c  o  m

  if (!isNaN(fixedNum) && fixedNum >= 0) {
    num = num.toFixed(fixedNum);
  }

  num = (num + '').split('.');

  for (var i = 0, len = num.length; i < len; i++) {
    while(re.test(num[i])) { 
      num[i] = num[i].replace(re, "$1,$2"); 
    }
  }

  return num.join('.');
};



PreviousNext

Related