Skip to content

Commit 1e57ceb

Browse files
ericyangpanclaude
andcommitted
feat(format): add token count formatting utility
Add formatTokenCount utility function to format large numbers with K/M suffixes for display. - Converts 32000 -> "32K" - Converts 128000 -> "128K" - Converts 1000000 -> "1M" - Handles edge cases and removes trailing .0 decimals This utility provides consistent formatting for token counts across the application UI. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 23fabe8 commit 1e57ceb

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/lib/format.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Formatting utility functions
3+
*/
4+
5+
/**
6+
* Format a large number with K/M suffix for display
7+
* Examples:
8+
* - 32000 -> "32K"
9+
* - 128000 -> "128K"
10+
* - 200000 -> "200K"
11+
* - 1000000 -> "1M"
12+
* - 4096 -> "4K"
13+
* @param value - The number to format
14+
* @returns Formatted string with K/M suffix
15+
*/
16+
export function formatTokenCount(value: number): string {
17+
if (value >= 1000000) {
18+
return `${(value / 1000000).toFixed(1)}M`.replace(/\.0$/, '')
19+
}
20+
if (value >= 1000) {
21+
return `${(value / 1000).toFixed(1)}K`.replace(/\.0$/, '')
22+
}
23+
return value.toString()
24+
}

0 commit comments

Comments
 (0)