fix: add shared number formatting utilities (formatMetric, formatCount, formatPercent)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
hsiegeln
2026-04-09 18:43:52 +02:00
parent 605c8ad270
commit 7ec56f3bd0
2 changed files with 19 additions and 5 deletions

View File

@@ -39,3 +39,20 @@ export function timeAgo(iso?: string): string {
if (hours < 24) return `${hours}h ago`;
return `${Math.floor(hours / 24)}d ago`;
}
export function formatMetric(value: number, unit: string, decimals = 1): string {
if (Math.abs(value) >= 1_000_000) return `${(value / 1_000_000).toFixed(decimals)}M ${unit}`;
if (Math.abs(value) >= 1_000) return `${(value / 1_000).toFixed(decimals)}K ${unit}`;
if (Number.isInteger(value)) return `${value} ${unit}`;
return `${value.toFixed(decimals)} ${unit}`;
}
export function formatCount(value: number): string {
if (value >= 1_000_000) return `${(value / 1_000_000).toFixed(1)}M`;
if (value >= 1_000) return `${(value / 1_000).toFixed(1)}K`;
return String(value);
}
export function formatPercent(value: number, decimals = 1): string {
return `${value.toFixed(decimals)}%`;
}