Created
January 23, 2020 08:44
-
-
Save tag1216/9684bf4534afae7b765b4d9fa6b8d275 to your computer and use it in GitHub Desktop.
format bytes
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
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