export default function parseLocaleNumber(numberString) { // Handle null, undefined, and empty values if (numberString === null || numberString === undefined || numberString === '') { return 0; } const str = String(numberString).trim(); if (str === '') { return 0; } // Remove any currency symbols, letters, and other non-numeric characters let cleaned = str.replace(/[^\d.,\-+]/g, ''); // Handle edge cases if (cleaned === '' || cleaned === '-' || cleaned === '+') { return 0; } // Count dots and commas const dotCount = (cleaned.match(/\./g) || []).length; const commaCount = (cleaned.match(/,/g) || []).length; try { // Determine format based on separators if (dotCount === 0 && commaCount === 0) { // Pure integer const result = parseInt(cleaned, 10); return isNaN(result) ? 0 : result; } else if (dotCount === 1 && commaCount === 0) { // US format (123.45) const result = parseFloat(cleaned); return isNaN(result) ? 0 : result; } else if (dotCount === 0 && commaCount === 1) { // Could be European (123,45) or US thousands (1,234) const parts = cleaned.split(','); if (parts.length === 2 && parts[1].length <= 2) { // Likely decimal (123,45) const result = parseFloat(cleaned.replace(',', '.')); return isNaN(result) ? 0 : result; } else { // Likely thousands separator (1,234) const result = parseFloat(cleaned.replace(',', '')); return isNaN(result) ? 0 : result; } } else { // Mixed separators - use position to determine const lastDot = cleaned.lastIndexOf('.'); const lastComma = cleaned.lastIndexOf(','); if (lastDot > lastComma) { // Dot is decimal separator (1,234.56) cleaned = cleaned.replace(/,/g, ''); } else { // Comma is decimal separator (1.234,56) cleaned = cleaned.replace(/\./g, '').replace(',', '.'); } const result = parseFloat(cleaned); return isNaN(result) ? 0 : result; } } catch (error) { return 0; } }