diff --git a/src/lib/format.ts b/src/lib/format.ts index e255566..0db5ed3 100644 --- a/src/lib/format.ts +++ b/src/lib/format.ts @@ -1,21 +1,12 @@ export function formatBytes(bytes: number, decimals: number = 2) { - if (!+bytes) return "0 Bytes"; + if (!+bytes) return "0 KiB"; const k = 1024; const dm = decimals < 0 ? 0 : decimals; - const sizes = [ - "Bytes", - "KiB", - "MiB", - "GiB", - "TiB", - "PiB", - "EiB", - "ZiB", - "YiB", - ]; + const sizes = ["KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]; - const i = Math.floor(Math.log(bytes) / Math.log(k)); + const i = Math.max(0, Math.floor(Math.log(bytes) / Math.log(k)) - 1); + const value = (bytes / k ** (i + 1)).toFixed(dm); - return `${parseFloat((bytes / k ** i).toFixed(dm))} ${sizes[i]}`; + return `${value} ${sizes[i]}`; }