Skip to content

Instantly share code, notes, and snippets.

@akotlov
Created August 6, 2017 18:46
Show Gist options
  • Save akotlov/5afa1f8545c756521174ae8741d77044 to your computer and use it in GitHub Desktop.
Save akotlov/5afa1f8545c756521174ae8741d77044 to your computer and use it in GitHub Desktop.
javascript convert bytes to size
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