Skip to content

Instantly share code, notes, and snippets.

@grosscorporation
Created December 3, 2017 17:07
Show Gist options
  • Save grosscorporation/6e29c28813d561dddd745ae56f4973eb to your computer and use it in GitHub Desktop.
Save grosscorporation/6e29c28813d561dddd745ae56f4973eb to your computer and use it in GitHub Desktop.
convert bytes to readable string
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