Last active
October 17, 2017 08:03
-
-
Save guillaumegarcia13/4ce4de76fd5108dd6a134e7fbdb6e0af to your computer and use it in GitHub Desktop.
Format File Size (0,98 KB, 1,5 GB, ...)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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