mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-10-31 08:25:35 +00:00
formatMultiplier rounded any value >= 0.01 with toFixed(2), so a configured multiplier like 0.035 displayed as 0.04x in the /usage and /admin/usage cost tooltips. Format with up to 4 decimals and trim trailing zeros while keeping at least 2 decimals; display-only fix, billing amounts were already computed from the true multiplier.
18 lines
627 B
TypeScript
18 lines
627 B
TypeScript
/**
|
||
* 格式化缓存 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()
|
||
}
|
||
|
||
/**
|
||
* 自适应精度格式化倍率:保留至多 4 位小数并去掉末尾多余的 0,
|
||
* 但至少保留 2 位小数(0.035 -> "0.035",0.3 -> "0.30",1 -> "1.00")
|
||
*/
|
||
export function formatMultiplier(val: number): string {
|
||
if (val < 0.0001) return val.toPrecision(2)
|
||
return val.toFixed(4).replace(/(\.\d{2}\d*?)0+$/, '$1')
|
||
}
|