Skip to content

Instantly share code, notes, and snippets.

@bitfishxyz
Created February 13, 2020 14:11
Show Gist options
  • Save bitfishxyz/fd49a6feb9f20986d4ae105f20e842a2 to your computer and use it in GitHub Desktop.
Save bitfishxyz/fd49a6feb9f20986d4ae105f20e842a2 to your computer and use it in GitHub Desktop.
function bytesToSize (bytes) {
if (bytes === 0) return '0 B';
var k = 1024;
var sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
var i = Math.floor(Math.log(bytes) / Math.log(k))
return (bytes / Math.pow(k, i)).toPrecision(3) + ' ' + sizes[i]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment