Skip to content

Instantly share code, notes, and snippets.

@walee-balogun
Forked from thewarpaint/filter.js
Created November 12, 2017 11:33
Show Gist options
  • Save walee-balogun/ff93e63d277b7d8256acfca67ed60e80 to your computer and use it in GitHub Desktop.
Save walee-balogun/ff93e63d277b7d8256acfca67ed60e80 to your computer and use it in GitHub Desktop.
Angular filter to convert numbers to thousand suffixes (1234 > 1.2k)
// Based on http://stackoverflow.com/questions/1571374/converting-values-to-unit-prefixes-in-jsp-page.
// The inner filter function can be used standalone.
angular.module('Utils')
.filter('thousandSuffix', function () {
return function (input, decimals) {
var exp, rounded,
suffixes = ['k', 'M', 'G', 'T', 'P', 'E'];
if(window.isNaN(input)) {
return null;
}
if(input < 1000) {
return input;
}
exp = Math.floor(Math.log(input) / Math.log(1000));
return (input / Math.pow(1000, exp)).toFixed(decimals) + suffixes[exp - 1];
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment