-
-
Save yahyaKacem/f6f17684180135ece8a6 to your computer and use it in GitHub Desktop.
Human Readable Numbers (AngularJS filter)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('humanize', []).filter('humanize', function() { | |
return function humanize(number) { | |
var si, exp, result; | |
if (number < 1000) { | |
return number; | |
} | |
si = ['K', 'M', 'G', 'T', 'P', 'H']; | |
exp = Math.floor(Math.log(number) / Math.log(1000)); | |
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]; | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment