Skip to content

Instantly share code, notes, and snippets.

@openam
Forked from thomseddon/gist:3511330
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save openam/3c46586001bef21f1e0f to your computer and use it in GitHub Desktop.

Select an option

Save openam/3c46586001bef21f1e0f to your computer and use it in GitHub Desktop.
AngularJS byte format filter
app.filter( 'bytes', function bytesFilter () {
'use strict';
return function bytesReturn ( bytes, precision ) {
if ( bytes === 0 ) {
return '0 bytes';
}
if ( isNaN( parseFloat( bytes ) ) || !isFinite( bytes ) ) {
return '-';
}
if ( typeof precision === 'undefined') {
precision = 1;
}
var units = [ 'bytes', 'kB', 'MB', 'GB', 'TB', 'PB' ];
var 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