fix(usage): format near-million token counts as millions (#39129)

Co-authored-by: CurryMessi <curry-messi@users.noreply.github.com>
This commit is contained in:
Peter Steinberger
2026-03-07 19:59:12 +00:00
parent 80a6eb3131
commit d6f28a3da7
3 changed files with 9 additions and 1 deletions

View File

@@ -12,6 +12,8 @@ describe("usage-format", () => {
expect(formatTokenCount(999)).toBe("999");
expect(formatTokenCount(1234)).toBe("1.2k");
expect(formatTokenCount(12000)).toBe("12k");
expect(formatTokenCount(999_499)).toBe("999k");
expect(formatTokenCount(999_500)).toBe("1.0m");
expect(formatTokenCount(2_500_000)).toBe("2.5m");
});

View File

@@ -25,7 +25,12 @@ export function formatTokenCount(value?: number): string {
return `${(safe / 1_000_000).toFixed(1)}m`;
}
if (safe >= 1_000) {
return `${(safe / 1_000).toFixed(safe >= 10_000 ? 0 : 1)}k`;
const precision = safe >= 10_000 ? 0 : 1;
const formattedThousands = (safe / 1_000).toFixed(precision);
if (Number(formattedThousands) >= 1_000) {
return `${(safe / 1_000_000).toFixed(1)}m`;
}
return `${formattedThousands}k`;
}
return String(Math.round(safe));
}