refactor: centralize number formatting with locale-aware utility

This commit is contained in:
2026-01-29 17:53:38 -06:00
parent d2985f98ce
commit 7a1cd2a39b
9 changed files with 53 additions and 30 deletions
+23
View File
@@ -8,3 +8,26 @@ export function cn(...inputs: ClassValue[]) {
/** Shared tooltip content styling for bits-ui Tooltip.Content */
export const tooltipContentClass =
"z-50 bg-card text-card-foreground text-xs border border-border rounded-md px-2.5 py-1.5 shadow-md max-w-72";
export interface FormatNumberOptions {
/** Include sign for positive numbers (default: false) */
sign?: boolean;
/** Maximum fraction digits (default: 0 for integers) */
maximumFractionDigits?: number;
}
/**
* Format a number with locale-aware thousands separators.
* Uses browser locale via Intl.NumberFormat.
*/
export function formatNumber(num: number, options: FormatNumberOptions = {}): string {
const { sign = false, maximumFractionDigits = 0 } = options;
const formatted = new Intl.NumberFormat(undefined, {
maximumFractionDigits,
}).format(num);
if (sign && num >= 0) {
return `+${formatted}`;
}
return formatted;
}