Created
December 3, 2017 17:07
-
-
Save grosscorporation/6e29c28813d561dddd745ae56f4973eb to your computer and use it in GitHub Desktop.
convert bytes to readable string
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 bytesToSize(bytes) { | |
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'] | |
if (bytes === 0) return 'n/a' | |
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10) | |
if (i === 0) return `${bytes} ${sizes[i]})` | |
return `${(bytes / (1024 ** i)).toFixed(1)} ${sizes[i]}` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment