Skip to content

Instantly share code, notes, and snippets.

@diegolamonica
Last active February 16, 2019 19:18
Show Gist options
  • Save diegolamonica/0d99c290c3d408ef96dd455a9c2380ed to your computer and use it in GitHub Desktop.
Save diegolamonica/0d99c290c3d408ef96dd455a9c2380ed to your computer and use it in GitHub Desktop.
Transforms Bytes measure unit into a human readable format
function humanReadableSize(bytes) {
var sizes = ['b', 'Kb', 'MB', 'GB', 'TB'],
transformed = bytes,
/*
* The lowest measure is byte
*/
index = 0;
/*
* Limits the conversion to managed sizes.
*/
while ((transformed > 1024) && (index < sizes.length-1) ) {
/*
* Reducing the byte value in something smaller
*/
transformed /= 1024;
/*
* Increase the measure unit;
*/
index++;
}
return (parseInt(transformed * 100)/100).toString() + sizes[index];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment