Skip to content

Instantly share code, notes, and snippets.

@zorzysty
Forked from thomseddon/gist:3511330
Last active October 28, 2015 16:22
Show Gist options
  • Save zorzysty/fdf2e64d6ee7dbde8c5e to your computer and use it in GitHub Desktop.
Save zorzysty/fdf2e64d6ee7dbde8c5e to your computer and use it in GitHub Desktop.
AngularJS byte format filter
"use strict";
(function () {
angular.module("backOffice")
.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)),
val = (bytes / Math.pow(1024, Math.floor(number))).toFixed(precision);
return (val.match(/\.0*$/) ? val.substr(0, val.indexOf('.')) : val) + ' ' + units[number];
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment