Skip to content

Instantly share code, notes, and snippets.

@wiedymi
Created December 12, 2019 12:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wiedymi/66de523220a84f4fa2e8bcf30a952ec2 to your computer and use it in GitHub Desktop.
Save wiedymi/66de523220a84f4fa2e8bcf30a952ec2 to your computer and use it in GitHub Desktop.
export const bytesToSize = (bytes, decimals = 2): string => {
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