Skip to content

Instantly share code, notes, and snippets.

@stefanocudini
Created October 6, 2021 18:41
Show Gist options
  • Save stefanocudini/46a1f068d66cef46aa6a7d3511d68bf5 to your computer and use it in GitHub Desktop.
Save stefanocudini/46a1f068d66cef46aa6a7d3511d68bf5 to your computer and use it in GitHub Desktop.
filesize human in nodejs/javascript
function filesize(bytes) {
if (bytes === 0) return bytes;
var sizes = ['Bytes','KB','MB','GB','TB'],
i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
return Math.round(bytes / Math.pow(1024, i), 1) + ' ' + sizes[i];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment