Skip to content

Instantly share code, notes, and snippets.

@jessecurry
Forked from thomseddon/gist:3511330
Last active December 4, 2015 10:08
Show Gist options
  • Save jessecurry/cd79fcc705cc3db97347 to your computer and use it in GitHub Desktop.
Save jessecurry/cd79fcc705cc3db97347 to your computer and use it in GitHub Desktop.
Angular filter to display file sizes
app.filter('bytes', function() {
return function(bytes, precision) {
if (isNaN(parseFloat(bytes)) || !isFinite(bytes)) {
return '-';
}
if (typeof precision === 'undefined') {
precision = 1;
}
var units = ['bytes', 'kB', 'MB', 'GB', 'TB', 'PB'],
number = Math.floor(Math.log(bytes) / Math.log(1024));
return (bytes / Math.pow(1024, Math.floor(number))).toFixed(precision) + ' ' + units[number];
}
});
@jessecurry
Copy link
Author

Updated with explicit braces and some whitespace to make it more readable (all of my .js is minified on deploy anyways).

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