Javascript Number format()

Introduction

Javascript Number format()

Number.prototype.format = function(){
  if(this==0) return 0;

  var reg = /(^[+-]?\d+)(\d{3})/;
  var n = (this + '');

  while (reg.test(n)) n = n.replace(reg, '$1' + ',' + '$2');

  return n;/*from   ww w.  jav  a2 s .c om*/
};

String.prototype.format = function(){
  var num = parseFloat(this);
  if( isNaN(num) ) return "0";

  return num.format();
};

var checkitout = {};

Javascript Number format()

Number.prototype.format = function(){
    if(this==0) return 0;
 
    var r = /(^[+-]?\d+)(\d{3})/;
    var n = (this + '');
 
    while (r.test(n)) n = n.replace(r, '$1' + ',' + '$2');
 
    return n;//from   w  w w . j  ava  2  s .c  o  m
};

Javascript Number format()

Number.prototype.format = function () {
  var str = this.toString();
  var Re = /[^0-9]/g;
  var ReN = /(-?[0-9]+)([0-9]{3})/;
  str = str.replace(Re,'');              
  while (ReN.test(str)) { 
    str = str.replace(ReN, "$1,$2"); 
  }/*from  w  w  w .ja va  2  s . com*/
  return str;
};

Date.prototype.hstr = function (format) {
 var m = this.getMonth()+1;
 var d = this.getDate();
 var h = this.getHours();
 var min = this.getMinutes();
 var s = this.getSeconds();
 return format.
   replace('Y', this.getFullYear()).
   replace('m', m.toString().length === 1 ? '0'+ m : m).
   replace('n', m).
   replace('d', d.toString().length === 1 ? '0'+ d : d).
   replace('j', d).
   replace('j', this.getDate()).
   replace('H', h.toString().length === 1 ? '0'+ h : h).
   replace('h', h).
   replace('i', min.toString().length === 1 ? '0'+ min : min).
   replace('s', s.toString().length === 1 ? '0'+ s : s);
};



PreviousNext

Related