Skip to content

Instantly share code, notes, and snippets.

@tag1216
Created January 23, 2020 08:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tag1216/9684bf4534afae7b765b4d9fa6b8d275 to your computer and use it in GitHub Desktop.
Save tag1216/9684bf4534afae7b765b4d9fa6b8d275 to your computer and use it in GitHub Desktop.
format bytes
const UNITS = [
"bytes",
"KB",
"MB",
"GB",
"TB",
];
export function formatBytes(bytes: number) {
const index = Math.min(Math.floor(Math.log10(bytes) / 3), UNITS.length - 1);
const value = Number((bytes / Math.pow(1000, index)).toPrecision(3));
return `${value.toLocaleString()} ${UNITS[index]}`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment