Skip to content

Instantly share code, notes, and snippets.

@napoli1890
Forked from thomseddon/gist:3511330
Last active April 14, 2016 09:26
Show Gist options
  • Save napoli1890/d33c7b9056e77a8670874e93da3e992b to your computer and use it in GitHub Desktop.
Save napoli1890/d33c7b9056e77a8670874e93da3e992b to your computer and use it in GitHub Desktop.
AngularJS Bytes Formatter Filter
app.filter('bytes', function() {
return function(bytes, precision) {
if (bytes == 0 || 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];
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment