Skip to content

Commit 003f3c8

Browse files
committed
fix(usage): show tiny positive shares as less than 0.1 percent
1 parent 3f510e0 commit 003f3c8

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

‎packages/shared/src/usageFormat.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,22 @@ import {
55
enumerateHourStarts,
66
formatDateTimeShort,
77
formatHourShort,
8+
formatPercent,
89
formatRelativeHourShort,
910
makeWindow,
1011
} from "./usageFormat.ts";
1112

13+
describe("formatPercent", () => {
14+
it("distinguishes a small positive share from zero", () => {
15+
expect(formatPercent(0)).toBe("0.0%");
16+
expect(formatPercent(0.0004)).toBe("<0.1%");
17+
expect(formatPercent(0.0009)).toBe("<0.1%");
18+
expect(formatPercent(0.001)).toBe("0.1%");
19+
expect(formatPercent(0.023)).toBe("2.3%");
20+
expect(formatPercent(0.00004, 2)).toBe("<0.01%");
21+
});
22+
});
23+
1224
describe("hourly usage formatting", () => {
1325
it("keeps requested zones separate when formatting repeated calls", () => {
1426
const instant = "2026-08-11T12:37:00.000Z";

‎packages/shared/src/usageFormat.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ function trim(value: number): string {
4343
}
4444

4545
export function formatPercent(share: number, digits = 1): string {
46-
return `${(share * 100).toFixed(digits)}%`;
46+
const percent = share * 100;
47+
const smallest = 10 ** -digits;
48+
if (percent > 0 && percent < smallest) return `<${smallest.toFixed(digits)}%`;
49+
return `${percent.toFixed(digits)}%`;
4750
}
4851

4952
/** `2026-08-07` to `Aug 7`. */

0 commit comments

Comments
 (0)