Skip to content

Instantly share code, notes, and snippets.

@onsummer
Created March 29, 2021 02:00
Show Gist options
  • Save onsummer/1e716ad59db94bbef2562ac5f2638665 to your computer and use it in GitHub Desktop.
Save onsummer/1e716ad59db94bbef2562ac5f2638665 to your computer and use it in GitHub Desktop.
return a string that friendly print file size.
function friendlyFileSize(sizeInBytes) {
if(sizeInBytes < 1024) {
return `${sizeInBytes} Bytes`;
} else if(sizeInBytes >= 1024 && sizeInBytes < 1048576) {
return `${(sizeInBytes/1024).toFixed(2)} KB`;
} else if(sizeInBytes >= 1048576) {
return `${(sizeInBytes/1048576).toFixed(2)} MB`;
}
}
// friendlyFileSize(123490421)
// "117.77 MB"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment