Skip to content

Instantly share code, notes, and snippets.

@maggiben
Last active October 21, 2018 11:35
Show Gist options
  • Save maggiben/9457434 to your computer and use it in GitHub Desktop.
Save maggiben/9457434 to your computer and use it in GitHub Desktop.
Human Readable Numbers (AngularJS filter)
angular.module('humanize', [])
.filter('humanize', function(){
return function humanize(number) {
if(number < 1000) {
return number;
}
var si = ['K', 'M', 'G', 'T', 'P', 'H'];
var exp = Math.floor(Math.log(number) / Math.log(1000));
var result = number / Math.pow(1000, exp);
result = (result % 1 > (1 / Math.pow(1000, exp - 1))) ? result.toFixed(2) : result.toFixed(0);
return result + si[exp - 1];
};
});
@maggiben
Copy link
Author

Wont print decimals if there below the tolerance (2)

@mennanov
Copy link

This function will not print a human readable output for a big negative number, for instance for -500000.

@diegolmello
Copy link

Great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment