Skip to content

Instantly share code, notes, and snippets.

@markuszeller
Forked from bitfishxyz/bytes.js
Last active June 21, 2023 10:24
Show Gist options
  • Save markuszeller/b61b757f97675b43063c0c5ee0714566 to your computer and use it in GitHub Desktop.
Save markuszeller/b61b757f97675b43063c0c5ee0714566 to your computer and use it in GitHub Desktop.
function bytesToSize (bytes) {
if (bytes === 0) return '0 B';
const k = 1024;
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const 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