Fixes traffic parsing for small values (#5737)

- Unified handler to IEC standard.
- Change handler to parse number in a unified style, even with number less than 1000.

Signed-off-by: Dragon1573 <49941141+Dragon1573@users.noreply.github.com>
This commit is contained in:
❤是纱雾酱哟~ 2025-12-06 09:00:04 +08:00 committed by GitHub
parent 7ae2414067
commit ff0de29f17
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,8 +2,8 @@ const UNITS = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
const parseTraffic = (num?: number) => {
if (typeof num !== "number") return ["NaN", ""];
if (num < 1000) return [`${Math.round(num)}`, "B"];
const exp = Math.min(Math.floor(Math.log2(num) / 10), UNITS.length - 1);
const exp =
num < 1 ? 0 : Math.min(Math.floor(Math.log2(num) / 10), UNITS.length - 1);
const dat = num / Math.pow(1024, exp);
const ret = dat >= 1000 ? dat.toFixed(0) : dat.toPrecision(3);
const unit = UNITS[exp];