refactor(ui): extract formatCacheTokens and formatMultiplier to shared utils
This commit is contained in:
18
frontend/src/utils/formatters.ts
Normal file
18
frontend/src/utils/formatters.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* 格式化缓存 token 数量(1K/1M 缩写)
|
||||
*/
|
||||
export function formatCacheTokens(tokens: number): string {
|
||||
if (tokens >= 1000000) return `${(tokens / 1000000).toFixed(1)}M`
|
||||
if (tokens >= 1000) return `${(tokens / 1000).toFixed(1)}K`
|
||||
return tokens.toLocaleString()
|
||||
}
|
||||
|
||||
/**
|
||||
* 自适应精度格式化倍率(确保小数值如 0.001 不被截断)
|
||||
*/
|
||||
export function formatMultiplier(val: number): string {
|
||||
if (val >= 0.01) return val.toFixed(2)
|
||||
if (val >= 0.001) return val.toFixed(3)
|
||||
if (val >= 0.0001) return val.toFixed(4)
|
||||
return val.toPrecision(2)
|
||||
}
|
||||
Reference in New Issue
Block a user