fix: fmtBytes 大文件显示成 PiB(累除逻辑错误)+ 改用 KB/MB/GB 单位
原实现循环里 n = x / 1024 每次都用原始 x 除一次,未累除,导致大文件 一路跌到 PiB 分支显示错误值。改为 while 循环每次 x /= 1000 累除, 单位改为 B/KB/MB/GB/TB/PB(1000 进制),B 显示整数其余保留 1 位小数。
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user