Last active
February 7, 2023 02:08
-
-
Save cuonghuynh/0cc9e70eae4b2c42ead0fcf1c0ab0dc3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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