Skip to content

Instantly share code, notes, and snippets.

@cuonghuynh
Last active February 7, 2023 02:08
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 cuonghuynh/0cc9e70eae4b2c42ead0fcf1c0ab0dc3 to your computer and use it in GitHub Desktop.
Save cuonghuynh/0cc9e70eae4b2c42ead0fcf1c0ab0dc3 to your computer and use it in GitHub Desktop.
function formatBytes(bytes: number): string {
const decimals = 2;
if (bytes === 0) return "0 B";
const k = 1024;
const dm: number = decimals < 0 ? 0 : decimals;
const sizes: string[] = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
const i: number = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment