Files
clash-verge-rev/src/utils/parse-traffic.ts
T
Alechron 75c921b466 fix(traffic): avoid scientific notation in traffic display near 1000 threshold (#7928)
* fix(traffic): avoid scientific notation in traffic display near 1000 threshold

When a traffic value in any unit (KB, MB, etc.) falls in the 999.5–1000
range, toPrecision(3) rounds it to 1000 but outputs scientific notation
"1.00e+3" instead of plain "1000". Check Math.round(dat) >= 1000 instead
of dat >= 1000 to catch this rounding boundary.

Signed-off-by: Alec <alechron@outlook.com>

* fix(traffic): avoid scientific notation in formatConnectionTraffic

Signed-off-by: Alec <alechron@outlook.com>

---------

Signed-off-by: Alec <alechron@outlook.com>
2026-09-15 05:17:35 +00:00

15 lines
447 B
TypeScript

const UNITS = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
const parseTraffic = (num?: number) => {
if (typeof num !== 'number') return ['NaN', '']
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 = Math.round(dat) >= 1000 ? dat.toFixed(0) : dat.toPrecision(3)
const unit = UNITS[exp]
return [ret, unit]
}
export default parseTraffic