Skip to content

Instantly share code, notes, and snippets.

@julianhille
Last active August 29, 2015 14:06
Show Gist options
  • Save julianhille/348ba88ba2b482f50086 to your computer and use it in GitHub Desktop.
Save julianhille/348ba88ba2b482f50086 to your computer and use it in GitHub Desktop.
javascript file size format human readable
function filesizeformat(bytes, binary, precision) {
/*
Javascript filesizeformater.
Inspired by jinja2 and some gists.
@version 1.0.0
@copyright 2014 Julian Hille
@author Julian Hille
*/
binary = typeof binary !== 'undefined' ? binary : false;
precision = typeof precision !== 'undefined' ? precision : 2;
var base = binary ? 1024 : 1000;
var prefixes = [
(binary ? 'KiB' : 'kB'),
(binary ? 'MiB' : 'MB'),
(binary ? 'GiB' : 'GB'),
(binary ? 'TiB' : 'TB'),
(binary ? 'PiB' : 'PB'),
(binary ? 'EiB' : 'EB'),
(binary ? 'ZiB' : 'ZB'),
(binary ? 'YiB' : 'YB')
]
if (!isFinite(bytes)) {
return '- Bytes';
}
else if (bytes == 1) {
return '1 Byte';
}
else if (bytes < base) {
return bytes + ' Bytes';
}
var index = Math.floor(Math.log(bytes) / Math.log(base));
return parseFloat((bytes / Math.pow(base, Math.floor(index))).toFixed(precision)).toString() + ' ' + prefixes[index-1];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment