Skip to content

Instantly share code, notes, and snippets.

@guillaumegarcia13
Last active October 17, 2017 08:03
Show Gist options
  • Save guillaumegarcia13/4ce4de76fd5108dd6a134e7fbdb6e0af to your computer and use it in GitHub Desktop.
Save guillaumegarcia13/4ce4de76fd5108dd6a134e7fbdb6e0af to your computer and use it in GitHub Desktop.
Format File Size (0,98 KB, 1,5 GB, ...)
let formatFileSize = (bytes) => {
if (bytes === 0) {
return '0 B';
}
const k = 1024,
dm = 3,
sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
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