Created
November 8, 2020 03:15
-
-
Save celsobessa/ccafc42e323f9880bb0558b123089563 to your computer and use it in GitHub Desktop.
Convert byte units to reasonable units
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
// https://medium.com/javascript-in-plain-english/7-javascript-utility-functions-to-improve-your-efficiency-79d27132f186 | |
function bytesToSize (bytes) { | |
if (bytes === 0) return '0 B'; | |
var k = 1024; | |
var sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] | |
var i = Math.floor(Math.log(bytes) / Math.log(k)) | |
return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment