Last active
February 16, 2019 19:18
-
-
Save diegolamonica/0d99c290c3d408ef96dd455a9c2380ed to your computer and use it in GitHub Desktop.
Transforms Bytes measure unit into a human readable format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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