Skip to content

Instantly share code, notes, and snippets.

@dansmith01
Last active August 28, 2018 19:59
Show Gist options
  • Save dansmith01/9991d1334a31fd777f02e7af09781904 to your computer and use it in GitHub Desktop.
Save dansmith01/9991d1334a31fd777f02e7af09781904 to your computer and use it in GitHub Desktop.
Convert bytes to human readable, keeping 3 significant figures.
function bytesSI(bytes) {
if (isNaN(parseFloat(bytes)) || !isFinite(bytes)) return bytes;
var suffix = ' Bytes';
var suffixes = [' KB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB'];
while (suffixes.length && bytes >= 1000) {
bytes = (bytes + 0.0001) / 1000;
suffix = suffixes.shift();
}
return bytes.toString().substr(0,4).concat(suffix).replace(". ", " ");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment