newbie
newbie

Reputation: 24635

Converting big numbers to bigger factors in JavaScript

I have counter and sometimes it can get very big number, so I need to convert numbers like:

1300 => 1.3K
1000000 => 1M

And so on. How is this possible in JavaScript?

Upvotes: 2

Views: 622

Answers (5)

DragonKnight
DragonKnight

Reputation: 1870

I came up to these answers late when I was looking for a clean way to do it but just in case if anyone was looking for copy pasting an angular filter doing that here is mine. same logic as above responses:)

just to make you sure for 001234 it returns 1k

html:

<div ng-repeat="d in data>
    <div class="numbers">{{d | KGMPformat}}</div>
</div>

js:

// controller: 
$scope.data = [123,12345,1234567,12345678,12345678901]
// filter: 
(function(app) {
    app.filter('KGMPformat', function() { 
        return function(value) {
            if (value<1e3)  return value;           
            if (value<1e6)  return (value - value%1e3)/1e3 + "K"; 
            if (value<1e9)  return (value - value%1e6)/1e6 + "G"; 
            if (value<1e12) return (value - value%1e9)/1e9 + "M";
            if (value<1e15) return (value - value%1e12)/1e12 + "P"; 
            return value;
        };
    });
})(myApp);

Upvotes: 0

Greg Burd
Greg Burd

Reputation: 866

I picked up a nifty bit of code when looking for something to abbreviate and label byte sizes (e.g. 1024 bytes -> "1 KB") and changed it just a bit to suit your needs (1000 -> "1 K").

function abbreviate_number(num) {
  var sizes = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
  if (num < 1000) return num;
  var i = parseInt(Math.floor(Math.log(num) / Math.log(1000)));
  return ((i == 0) ? (num / Math.pow(1000, i)) : (num / Math.pow(1000, i)).toFixed(1)) + ' ' + sizes[i]; // use .round() in place of .toFixed if you don't want the decimal
};

Just for reference, here's what I needed.

function format_bytes(bytes) {
  var sizes = ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
  if (bytes == 0) return '';
  if (bytes == 1) return '1 Byte';
  var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
  return ((i == 0)? (bytes / Math.pow(1024, i)) : (bytes / Math.pow(1024, i)).toFixed(1)) + ' ' + sizes[i];
};

Upvotes: 1

Groovetrain
Groovetrain

Reputation: 3325

You could just use a classic cascading number formatter of sorts

function format_num(num) {
  if( num < 1000 )
    return num;
  else if( num < 1000000 )
    return parseInt(num / 1000) + "K";
  else if( num < 1000000000 )
    return parseInt(num / 1000000) + "M";
  //....
}

You just have to take care of rounding appropriately.

Upvotes: 0

Toto
Toto

Reputation: 91385

// Truncate a number to ind decimal places
function truncNb(Nb, ind) {
  var _nb = Nb * (Math.pow(10,ind));
  _nb = Math.floor(_nb);
  _nb = _nb / (Math.pow(10,ind));
  return _nb;
}
// convert a big number to k,M,G
function int2roundKMG(val) {
  var _str = "";
  if (val >= 1e9)        { _str = truncNb((val/1e9), 1) + ' G';
  } else if (val >= 1e6) { _str = truncNb((val/1e6), 1) + ' M';
  } else if (val >= 1e3) { _str = truncNb((val/1e3), 1) + ' k';
  } else { _str = parseInt(val);
  }
  return _str;
}

Upvotes: 4

schnaader
schnaader

Reputation: 49719

Pseudocode:

factor = 0;
while (value > 1000) {
  value = value/1000;
  factor++;
}
output value (you might perhaps limit output to 3 decimal places)
convert factor to unit and output (0 = none, 1 = K, 2 = M, 3 = G...)

Upvotes: 0

Related Questions