Nodejs Number Format format(k, fixLength)

Here you can find the source of format(k, fixLength)

Method Source Code

// PLUGINS ////  ww w. j av a 2  s. c  o  m
Number.decPoint = ',';
Number.thousand_sep = '.';

Number.prototype.format = function (k, fixLength) {
    if (!k) k = 0;
    var neu = '';
    var sign = this < 0 ? '-' : '';

    // Runden
    var f = Math.pow(10, k);
    var zahl = Math.abs(this);
    zahl = '' + parseInt(zahl * f + .5) / f;

    // Komma ermittlen
    var idx = zahl.indexOf('.');
    // fehlende Nullen einf?    if (fixLength && k) {
        zahl += (idx == -1 ? '.' : '' )
            + f.toString().substring(1);
    }

    // Nachkommastellen ermittlen
    idx = zahl.indexOf('.');
    if (idx == -1) idx = zahl.length;
    else neu = Number.decPoint + zahl.substr(idx + 1, k);

    // Tausendertrennzeichen
    while (idx > 0) {
        if (idx - 3 > 0)
            neu = Number.thousand_sep + zahl.substring(idx - 3, idx) + neu;
        else
            neu = zahl.substring(0, idx) + neu;
        idx -= 3;
    }
    return sign + neu;
};

Related

  1. 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"); 
      return str;
    ...
    
  2. 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");
    ...
    
  3. 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;
    ...
    
  4. 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);
    };
    
  5. 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) {
    ...
    
  6. format(n, x)
    Number.prototype.format = function(n, x) {
      var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\.' : '$') + ')';
      return this.toFixed(Math.max(0, ~~n)).replace(new RegExp(re, 'g'), '$&,');
    };
    
  7. format(n, x)
    Number.prototype.format = function(n, x) {
        var re = '(\\d)(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\.' : '$') + ')';
        return this.toFixed(Math.max(0, ~~n)).replace(new RegExp(re, 'g'), '$1,');
    };
    
  8. format(n, x)
    Number.prototype.format = function (n, x) {
        var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\.' : '$') + ')';
        return this.toFixed(Math.max(0, ~~n));
    };
    
  9. format(n, x, s, c)
    Number.isInteger = Number.isInteger || function(value) {
      return typeof value === 'number' && 
        isFinite(value) && 
        Math.floor(value) === value;
    };
    Number.prototype.format = function(n, x, s, c) {
        var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\D' : '$') + ')',
            num = this.toFixed(Math.max(0, ~~n));
        return (c ? num.replace('.', c) : num).replace(new RegExp(re, 'g'), '$&' + (s || ','));
    ...