Skip to content

Instantly share code, notes, and snippets.

@liyu1981
Forked from thomseddon/gist:3511330
Last active August 29, 2015 14:01
Show Gist options
  • Save liyu1981/06338d33a5947851918d to your computer and use it in GitHub Desktop.
Save liyu1981/06338d33a5947851918d to your computer and use it in GitHub Desktop.
Angularjs filter for: number => bytes/kB/MB/GB/TB/PB
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