Skip to content

Instantly share code, notes, and snippets.

@ma-9
Created February 28, 2020 07:17
Show Gist options
  • Save ma-9/7957fcc5db5819c4f3865e97733d786b to your computer and use it in GitHub Desktop.
Save ma-9/7957fcc5db5819c4f3865e97733d786b to your computer and use it in GitHub Desktop.
A Helper function to Return Size in Bytes, KB, MB, GB AND TB
function bytesToSize(bytes) {
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes == 0) return '0 Byte';
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment