Nodejs Number Format format()

Here you can find the source of format()

Method Source Code

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 .j  ava 2 s.co  m
  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);
};

Related

  1. addTrailingZero()
    Number.prototype.addTrailingZero = function(){
      if(this < 10)  return "0"+this;
      else  return this;
    };
    
  2. addZero()
    Number.prototype.addZero=function(){
        var val=(this+"");
        val=((2>val.length)?("0"+val):val);
        return val;
    
  3. comma()
    var d3 = /(\d+)(\d{3})/;
    Number.prototype.comma = function() {
        var s = this.toString();
        while (d3.test(s)) s = s.replace(d3, '$1,$2');
        return s;
    };
    
  4. commafy()
    Number.prototype.commafy = function () {
      return String(this).commafy();
    
  5. commafy()
    Number.prototype.commafy = function () {
      return String(this).commafy();
    String.prototype.commafy = function () {
      return this.replace(/(^|[^\w.])(\d{4,})/g, function($0, $1, $2) {
        return $1 + $2.replace(/\d(?=(?:\d\d\d)+(?!\d))/g, "$&,");
      });
    
  6. format()
    Number.prototype.format = function() {
      var n = this, decPlaces = 2, decSeparator = ".", thouSeparator = ",", sign = n < 0 ? "-" : "", i = parseInt( n = Math.abs(+n || 0).toFixed(decPlaces)) + "", j = ( j = i.length) > 3 ? j % 3 : 0;
      return sign + ( j ? i.substr(0, j) + thouSeparator : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thouSeparator) + ( decPlaces ? decSeparator + Math.abs(n - i).toFixed(decPlaces).slice(2) : "");
    };
    function isNumber(n) {
      return !isNaN(parseFloat(n)) && isFinite(n);
    var dayNames = new Array("Duminica", "Luni", "Marti", "Miercuri", "Joi", "Vineri", "Sambata");
    var monthNames = new Array("Ianuarie", "Februarie", "Martie", "Aprilie", "Mai", "Iunie", "Iulie", "August", "Septembrie", "Octobmrie", "Noiembrie", "Decembrie");
    ...
    
  7. format(decimalPoints,thousandsSep,decimalSep)
    Number.prototype.format=function(decimalPoints,thousandsSep,decimalSep){
      var val=this+'',re=/^(-?)(\d+)/,x,y;
      if (decimalPoints!=null) val = this.toFixed(decimalPoints);
      if (thousandsSep && (x=re.exec(val))){
        for (var a=x[2].split(''),i=a.length-3;i>0;i-=3) a.splice(i,0,thousandsSep);
        val=val.replace(re,x[1]+a.join(''));
      if (decimalSep) val=val.replace(/\./,decimalSep);
      return val;
    ...
    
  8. format(digit, separator)
    Number.prototype.format = function(digit, separator)
        if (typeof digit != 'number' || digit > 99) 
            throw new Error('Number.toFormattedString - Wrong argument.');
        var decimals = Math.round(((this - Math.floor(this)) * Math.pow(10, digit))).toString();
        while (digit - decimals.length > 0) 
            decimals = '0' + decimals;
        if (typeof separator != 'string' || separator.length != 1) 
            separator = '.';
        return Math.floor(this).toString() + separator + decimals.substr(0, digit);
    };
    
  9. format(fmt)
    Number.prototype.format = function (fmt) {
        var s = this.toFixed(2).toString();
        var decimals = "";
        if (s.indexOf(".") > 0) {
            var arr = s.split('.');
            s = arr[0]; decimals = arr[1];
        if (this > 0 && this < 1000) {
            if (decimals != "" && parseInt(decimals) != 0) {
    ...