fix(webui): update file size formatting to SI decimal standard

This commit is contained in:
yiling
2026-07-29 22:43:30 +08:00
parent c61a23c0c1
commit a64ed2aa13
3 changed files with 49 additions and 12 deletions
+17 -4
View File
@@ -1,7 +1,20 @@
export function formatSize(bytes: number): string {
if (bytes === 0) return '0 B'
const k = 1024
if (!bytes || bytes <= 0) return '0 B'
const k = 1000
const sizes = ['B', '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(1)) + ' ' + sizes[i]
let i = Math.floor(Math.log(bytes) / Math.log(k))
if (i < 0) i = 0
if (i >= sizes.length) i = sizes.length - 1
const val = bytes / Math.pow(k, i)
if (i === 0) {
return `${Math.round(val)} B`
}
const formatted = parseFloat(val.toFixed(2))
if (formatted >= 1000 && i < sizes.length - 1) {
return parseFloat((bytes / Math.pow(k, i + 1)).toFixed(2)) + ' ' + sizes[i + 1]
}
return `${formatted} ${sizes[i]}`
}