Skip to content

Instantly share code, notes, and snippets.

@MacKentoch
Created December 10, 2016 19:21
Show Gist options
  • Select an option

  • Save MacKentoch/83d25036dc521d4a0d8b16cfeeeb1c5e to your computer and use it in GitHub Desktop.

Select an option

Save MacKentoch/83d25036dc521d4a0d8b16cfeeeb1c5e to your computer and use it in GitHub Desktop.
export function formatBytes(bytes, decimals) {
if (bytes === 0) {
return '0 Byte';
}
const k = 1000; // or 1024 for binary
const dm = decimals + 1 || 3;
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