From 7bacc3367921a11d941de20723c7ed14c1c5cc02 Mon Sep 17 00:00:00 2001 From: zikai Date: Tue, 21 Jul 2026 14:52:44 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20fmtBytes=20=E5=A4=A7=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E6=88=90=20PiB=EF=BC=88=E7=B4=AF=E9=99=A4?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E9=94=99=E8=AF=AF=EF=BC=89+=20=E6=94=B9?= =?UTF-8?q?=E7=94=A8=20KB/MB/GB=20=E5=8D=95=E4=BD=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原实现循环里 n = x / 1024 每次都用原始 x 除一次,未累除,导致大文件 一路跌到 PiB 分支显示错误值。改为 while 循环每次 x /= 1000 累除, 单位改为 B/KB/MB/GB/TB/PB(1000 进制),B 显示整数其余保留 1 位小数。 --- static/common.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/static/common.js b/static/common.js index 64c272c..ac8c3c8 100644 --- a/static/common.js +++ b/static/common.js @@ -61,15 +61,15 @@ } function fmtBytes(n) { - if (n == null) return "-"; - const x = Number(n); - if (!isFinite(x)) return "-"; - for (const unit of ["B", "KiB", "MiB", "GiB", "TiB", "PiB"]) { - if (Math.abs(x) < 1024 || unit === "PiB") - return unit === "B" ? `${x} B` : `${x.toFixed(1)} ${unit}`; - n = x / 1024; + let x = Number(n); + if (n == null || !isFinite(x)) return "-"; + const units = ["B", "KB", "MB", "GB", "TB", "PB"]; + let i = 0; + while (Math.abs(x) >= 1000 && i < units.length - 1) { + x /= 1000; + i++; } - return `${n.toFixed(1)} PiB`; + return i === 0 ? `${Math.round(x)} ${units[i]}` : `${x.toFixed(1)} ${units[i]}`; } function fmtTime(s) {