- Consolidate common utilities (cn, format, time) into lib/utils - Remove redundant utility files from pastel and units directories - Clean up unused components (Separator, KeyboardShortcutsHelp) - Relocate CommandPalette to components/units/ui/ - Force dark mode on landing page and improve theme persistence logic - Add FOUC prevention script to RootLayout - Fix sidebar height constraint in AppShell
55 lines
1.4 KiB
TypeScript
55 lines
1.4 KiB
TypeScript
/**
|
|
* Format a number for display with proper precision
|
|
*/
|
|
export function formatNumber(
|
|
value: number,
|
|
options: {
|
|
maxDecimals?: number;
|
|
minDecimals?: number;
|
|
notation?: 'standard' | 'scientific' | 'engineering' | 'compact';
|
|
} = {}
|
|
): string {
|
|
const {
|
|
maxDecimals = 6,
|
|
minDecimals = 0,
|
|
notation = 'standard',
|
|
} = options;
|
|
|
|
// Handle edge cases
|
|
if (!isFinite(value)) return value.toString();
|
|
if (value === 0) return '0';
|
|
|
|
// Use scientific notation for very large or very small numbers
|
|
const absValue = Math.abs(value);
|
|
const useScientific =
|
|
notation === 'scientific' ||
|
|
(notation === 'standard' && (absValue >= 1e10 || absValue < 1e-6));
|
|
|
|
if (useScientific) {
|
|
return value.toExponential(maxDecimals);
|
|
}
|
|
|
|
// Format with appropriate decimal places
|
|
const formatted = new Intl.NumberFormat('en-US', {
|
|
minimumFractionDigits: minDecimals,
|
|
maximumFractionDigits: maxDecimals,
|
|
notation: notation === 'compact' ? 'compact' : 'standard',
|
|
}).format(value);
|
|
|
|
return formatted;
|
|
}
|
|
|
|
/**
|
|
* Parse a number input string
|
|
*/
|
|
export function parseNumberInput(input: string): number | null {
|
|
if (!input || input.trim() === '') return null;
|
|
|
|
// Remove spaces and replace comma with dot
|
|
const cleaned = input.replace(/\s/g, '').replace(',', '.');
|
|
|
|
const parsed = parseFloat(cleaned);
|
|
|
|
return isNaN(parsed) ? null : parsed;
|
|
}
|