Created
December 10, 2016 19:21
-
-
Save MacKentoch/83d25036dc521d4a0d8b16cfeeeb1c5e 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
| export function formatBytes(bytes, decimals) { | |
| if (bytes === 0) { | |
| return '0 Byte'; | |
| } | |
| const k = 1000; // or 1024 for binary | |
| const dm = decimals + 1 || 3; | |
| const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; | |
| const i = 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