fix: parse bytes precision, close #334
This commit is contained in:
parent
d5037f180e
commit
e7f294a065
@ -3,7 +3,8 @@ const UNITS = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
|
|||||||
const parseTraffic = (num: number) => {
|
const parseTraffic = (num: number) => {
|
||||||
if (num < 1000) return [`${Math.round(num)}`, "B"];
|
if (num < 1000) return [`${Math.round(num)}`, "B"];
|
||||||
const exp = Math.min(Math.floor(Math.log2(num) / 10), UNITS.length - 1);
|
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];
|
const unit = UNITS[exp];
|
||||||
|
|
||||||
return [ret, unit];
|
return [ret, unit];
|
||||||
|
Loading…
Reference in New Issue
Block a user