Skip to content

Instantly share code, notes, and snippets.

@logzh
Forked from thomseddon/gist:3511330
Created April 21, 2017 07:42
Show Gist options
  • Save logzh/9319c90841447144bf43c183862f5ccb to your computer and use it in GitHub Desktop.
Save logzh/9319c90841447144bf43c183862f5ccb to your computer and use it in GitHub Desktop.
AngularJS byte format filter
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];
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment