Nodejs Number Format format(decimalPoints,thousandsSep,decimalSep)

Here you can find the source of format(decimalPoints,thousandsSep,decimalSep)

Method Source Code

//*** This code is copyright 2004 by Gavin Kistner, gavin@refinery.com
//*** It is covered under the license viewable at http://phrogz.net/JS/_ReuseLicense.txt
//*** Reuse or modification is free provided you abide by the terms of that license.
//*** (Including the first two lines above in your source code mostly satisfies the conditions.)


// Rounds a number to a specified number of decimals (optional)
// Inserts the character of your choice as the thousands separator (optional)
// Uses the character of your choice for the decimals separator (optional)
////from   w  w  w  . j  a  v  a 2  s.  com
// It's not a highly optimized speed demon, but it gives the right result...
// ...do you really care how speedy it is? :)
//
// !!Note!! IEWin gets (-0.007).format(2) WRONG, claiming that it's "0.00"
// This is a bug in IEWin's Number.toFixed() function.


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;
}
if (typeof Number.prototype.toFixed!='function' || (.9).toFixed()=='0' || (.007).toFixed(2)=='0.00') Number.prototype.toFixed=function(f){
   if (isNaN(f*=1) || f<0 || f>20) f=0;
   var s='',x=this.valueOf(),m='';
   if (this<0){ s='-'; x*=-1; }
   if (x>=Math.pow(10,21)) m=x.toString();
   else{
      m=Math.round(Math.pow(10,f)*x).toString();
      if (f!=0){
         var k=m.length;
         if (k<=f){
            var z='00000000000000000000'.substring(0,f+1-k);
            m=z+m;
            k=f+1;
         }
         var a = m.substring(0,k-f);
         var b = m.substring(k-f);
         m = a+'.'+b;
      }
   }
   if (m=='0') s='';
   return s+m;
}



// var x = 1234567.89532;
// x.format()                  => 1234567.89532
// x.format(2)                 => 1234567.90
// x.format(2,',')             => 1,234,567.90
// x.format(0,',')             => 1,234,568
// x.format(null,',')          => 1,234,567.89532
// x.format(null,' ',',')      => 1 234 567,89532
// x.format(2,' ',',')         => 1 234 567,90

Related

  1. 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;
    };
    
  2. commafy()
    Number.prototype.commafy = function () {
      return String(this).commafy();
    
  3. 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, "$&,");
      });
    
  4. 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;
    ...
    
  5. 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");
    ...
    
  6. 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);
    };
    
  7. 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) {
    ...
    
  8. format(k, fixLength)
    Number.decPoint = ',';
    Number.thousand_sep = '.';
    Number.prototype.format = function (k, fixLength) {
        if (!k) k = 0;
        var neu = '';
        var sign = this < 0 ? '-' : '';
        var f = Math.pow(10, k);
        var zahl = Math.abs(this);
        zahl = '' + parseInt(zahl * f + .5) / f;
    ...
    
  9. 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'), '$&,');
    };