* 将字节格式化为可读大小 * @param {number} bytes - 字节数 * @returns {string} 格式化后的字符串
(bytes)
| 295 | * @returns {string} 格式化后的字符串 |
| 296 | */ |
| 297 | function formatBytes(bytes) { |
| 298 | if (bytes === 0) return '0 B'; |
| 299 | |
| 300 | const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']; |
| 301 | const i = Math.floor(Math.log(bytes) / Math.log(1024)); |
| 302 | |
| 303 | return parseFloat((bytes / Math.pow(1024, i)).toFixed(2)) + ' ' + sizes[i]; |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * 格式化运行时间 |
no outgoing calls
no test coverage detected