Skip to content

Commit 682a04c

Browse files
🤖 feat: add millions unit to token formatting (#1211)
Update `formatTokens` to display `M` suffix for values >= 1M tokens. Previously showed `58507.9k` for ~58M tokens, now shows `58.5M`. --- _Generated with `mux` • Model: `anthropic:claude-opus-4-5` • Thinking: `high`_
1 parent 1b63729 commit 682a04c

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { describe, expect, test } from "bun:test";
2+
import { formatTokens } from "./tokenMeterUtils";
3+
4+
describe("formatTokens", () => {
5+
test("formats small numbers as-is with locale formatting", () => {
6+
expect(formatTokens(0)).toBe("0");
7+
expect(formatTokens(500)).toBe("500");
8+
expect(formatTokens(999)).toBe("999");
9+
});
10+
11+
test("formats thousands with k suffix", () => {
12+
expect(formatTokens(1000)).toBe("1.0k");
13+
expect(formatTokens(1500)).toBe("1.5k");
14+
expect(formatTokens(58507)).toBe("58.5k");
15+
expect(formatTokens(999_999)).toBe("1000.0k");
16+
});
17+
18+
test("formats millions with M suffix", () => {
19+
expect(formatTokens(1_000_000)).toBe("1.0M");
20+
expect(formatTokens(1_500_000)).toBe("1.5M");
21+
expect(formatTokens(58_507_900)).toBe("58.5M");
22+
expect(formatTokens(4_133_000)).toBe("4.1M");
23+
});
24+
});

‎src/common/utils/tokens/tokenMeterUtils.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,13 @@ export function calculateTokenMeterData(
100100
}
101101

102102
export function formatTokens(tokens: number): string {
103-
return tokens >= 1000 ? `${(tokens / 1000).toFixed(1)}k` : tokens.toLocaleString();
103+
if (tokens >= 1_000_000) {
104+
return `${(tokens / 1_000_000).toFixed(1)}M`;
105+
}
106+
if (tokens >= 1_000) {
107+
return `${(tokens / 1_000).toFixed(1)}k`;
108+
}
109+
return tokens.toLocaleString();
104110
}
105111

106112
export function getSegmentLabel(type: TokenSegment["type"]): string {

0 commit comments

Comments
 (0)