diff --git a/apps/web/src/lib/formatBytes.test.ts b/apps/web/src/lib/formatBytes.test.ts index 5b5d948bb59c..82de7bb7267c 100644 --- a/apps/web/src/lib/formatBytes.test.ts +++ b/apps/web/src/lib/formatBytes.test.ts @@ -8,6 +8,8 @@ describe("formatBytes", () => { [1_023, "1023 B"], [1_024, "1.00 KB"], [1_536, "1.50 KB"], + [10_235, "10.0 KB"], + [102_350, "100 KB"], [1_047_552, "1023 KB"], [1_048_576, "1.00 MB"], [5 * 1_024 ** 4, "5.00 TB"], diff --git a/apps/web/src/lib/formatBytes.ts b/apps/web/src/lib/formatBytes.ts index fe6de1cdb2a1..1c1e1111f0d9 100644 --- a/apps/web/src/lib/formatBytes.ts +++ b/apps/web/src/lib/formatBytes.ts @@ -1,5 +1,9 @@ function formatScaled(value: number): string { - return value.toFixed(value >= 100 ? 0 : value >= 10 ? 1 : 2); + const twoDecimals = value.toFixed(2); + if (Number(twoDecimals) < 10) return twoDecimals; + const oneDecimal = value.toFixed(1); + if (Number(oneDecimal) < 100) return oneDecimal; + return value.toFixed(0); } export function formatBytes(value: number): string {