Files
sub2api/frontend/src/utils/formatters.ts
T
shaw d0bdd7e771 fix(usage): keep significant decimals in cost tooltip rate multiplier
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.
2026-07-22 14:57:43 +08:00

18 lines
627 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 格式化缓存 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')
}