Created
December 20, 2020 15:39
-
-
Save Ref-Bit/e161817f1507f7db192308bae0d03f98 to your computer and use it in GitHub Desktop.
Convert Bytes number into (KB, MB, GB, TB,...) depending on its size.
This file contains hidden or 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
const formatBytes = (bytes, decimals = 2) => { | |
if (bytes === 0) return '0 Bytes'; | |
const k = 1024; | |
const dm = decimals < 0 ? 0 : decimals; | |
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; | |
const i = Math.floor(Math.log(bytes) / Math.log(k)); | |
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment