/** * Format numeric values for UI display without long conversion tails. * Whole numbers remain whole; non-integers are rounded to maxDecimals. */ export function formatDisplayNumber(value: number, maxDecimals = 2): string { if (!Number.isFinite(value)) { return '0'; } const factor = Math.pow(10, maxDecimals); const rounded = Math.round((value + Number.EPSILON) * factor) / factor; return Number.isInteger(rounded) ? String(rounded) : rounded .toFixed(maxDecimals) .replace(/\.0+$/, '') .replace(/(\.\d*?)0+$/, '$1'); }