Javascript String numberWithCommas()

Description

Javascript String numberWithCommas()


String.prototype.numberWithCommas = function () {
 if (typeof parseFloat(this) === "number") {
  thiss = (Math.round((this) * 100) / 100).toString();
  var dotLastIndex = this.lastIndexOf(".");
  if (dotLastIndex === -1) {
   thiss = this + ".00"
  } else {//from w w  w  . j  ava2  s  .c o  m
   var decimal = this.substring(dotLastIndex+1);
   var nonDecimal = this.substring(0, dotLastIndex);
   thiss = nonDecimal + "." + decimal;

   if (this[this.lastIndexOf(".")+2] == undefined) {
    thiss = this + "0"
   }
  }

  var parts = this.toString().split(",");
  parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  return parts.join(".");
 } else {
  console.log(this, ": please only number")
 }
};



PreviousNext

Related