fix the thousand and decimal ambiguity

This commit is contained in:
Suherdy Yacob 2026-01-09 13:38:32 +07:00
parent ea0fee1b45
commit 0739fd2a5f

View File

@ -19,14 +19,14 @@ function safelyApplyDecimalStyling(element) {
// Mark as processed to avoid conflicts // Mark as processed to avoid conflicts
element.setAttribute('data-decimal-processed', 'true'); element.setAttribute('data-decimal-processed', 'true');
// Pattern for decimal numbers (including currency formats) // Improved patterns that properly distinguish thousands separators from decimal separators
const patterns = [ const patterns = [
// Currency with Rp: Rp 6,210,000.00 // Currency with decimal: Rp 6,210,000.00 (comma for thousands, dot for decimal)
/^(.*Rp\s*)(\d{1,3}(?:[.,]\d{3})*)[.,](\d+)(.*)$/, /^(.*Rp\s*)(\d{1,3}(?:,\d{3})*)\.(\d{2})(.*)$/,
// Standard decimal: 123.45, 1,234.56 // Standard decimal with thousands: 1,234.56 (comma for thousands, dot for decimal)
/^(.*)(\d{1,3}(?:[.,]\d{3})*)[.,](\d+)(.*)$/, /^(.*)(\d{1,3}(?:,\d{3})*)\.(\d+)(.*)$/,
// Simple decimal: 240.000 // Simple decimal without thousands: 240.000, 123.45
/^(.*)(\d+)[.](\d+)(.*)$/ /^(.*)(\d+)\.(\d+)(.*)$/
]; ];
for (const pattern of patterns) { for (const pattern of patterns) {
@ -46,11 +46,13 @@ function safelyApplyDecimalStyling(element) {
} }
// Handle whole numbers in quantity contexts (be more selective) // Handle whole numbers in quantity contexts (be more selective)
if (text.match(/^\d+$/) && text.length > 0 && ( // Only add decimals to numbers that are clearly quantities, not prices with thousands separators
if (text.match(/^\d+$/) && text.length > 0 && text.length <= 4 && (
element.closest('[name="quantity"]') || element.closest('[name="quantity"]') ||
(element.closest('td') && element.closest('table') && text.length <= 6) // Only for reasonable quantities element.closest('[name="quantity_done"]') ||
element.closest('[name="reserved_availability"]')
)) { )) {
console.log('Safe Decimal Observer: Adding decimals to whole number', text); console.log('Safe Decimal Observer: Adding decimals to quantity', text);
// Double-check before updating // Double-check before updating
if (element.textContent.trim() === text && document.contains(element)) { if (element.textContent.trim() === text && document.contains(element)) {
@ -59,6 +61,13 @@ function safelyApplyDecimalStyling(element) {
} }
} }
// Do NOT process numbers with commas that don't have decimal points
// These are likely thousands separators (e.g., 8,500 should stay as 8,500)
if (text.match(/^\d{1,3}(?:,\d{3})+$/) && !text.includes('.')) {
console.log('Safe Decimal Observer: Skipping thousands separator number', text);
return false;
}
} catch (error) { } catch (error) {
console.warn('Safe decimal styling error (non-critical):', error); console.warn('Safe decimal styling error (non-critical):', error);
// Remove the processing flag if there was an error // Remove the processing flag if there was an error