Skip to content

Instantly share code, notes, and snippets.

@eddiemonge
Forked from thomseddon/gist:3511330
Last active December 22, 2015 12:08
Show Gist options
  • Save eddiemonge/6470132 to your computer and use it in GitHub Desktop.
Save eddiemonge/6470132 to your computer and use it in GitHub Desktop.
angular.module('filterHelper', []);
angular.module('filterHelper').filter('bytes', function() {
return function(bytes, precision) {
var parsedBytes = parseFloat(bytes);
if (isNaN(parsedBytes) || !isFinite(bytes)) {
return '--';
}
if ( parsedBytes === 0 ) {
return '0';
}
precision = precision || 1;
var units = ['bytes', 'kB', 'MB', 'GB', 'TB', 'PB'];
var number = Math.floor( Math.log(bytes) / Math.log(1024) );
return (bytes / Math.pow(1024, Math.floor(number))).toFixed(precision) + ' ' + units[number];
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment