From e7f294a065cf7a9bbe0c9b723e9ce584e4445689 Mon Sep 17 00:00:00 2001 From: GyDi Date: Wed, 11 Jan 2023 14:05:49 +0800 Subject: [PATCH] fix: parse bytes precision, close #334 --- src/utils/parse-traffic.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/utils/parse-traffic.ts b/src/utils/parse-traffic.ts index 6bb1a8c..49afb6e 100644 --- a/src/utils/parse-traffic.ts +++ b/src/utils/parse-traffic.ts @@ -3,7 +3,8 @@ const UNITS = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]; const parseTraffic = (num: number) => { if (num < 1000) return [`${Math.round(num)}`, "B"]; const exp = Math.min(Math.floor(Math.log2(num) / 10), UNITS.length - 1); - const ret = (num / Math.pow(1024, exp)).toPrecision(3); + const dat = num / Math.pow(1024, exp); + const ret = dat >= 1000 ? dat.toFixed(0) : dat.toPrecision(3); const unit = UNITS[exp]; return [ret, unit];