(bytes: number, decimals = 2)
| 144 | } |
| 145 | |
| 146 | export function formatBytes(bytes: number, decimals = 2): string { |
| 147 | if (bytes === 0) return "0 Bytes"; |
| 148 | |
| 149 | const k = 1024; |
| 150 | const dm = decimals < 0 ? 0 : decimals; |
| 151 | const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; |
| 152 | |
| 153 | const i = Math.floor(Math.log(bytes) / Math.log(k)); |
| 154 | |
| 155 | return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i]; |
| 156 | } |