Files
sure/app/javascript/utils/parse_locale_float.js
Claude 144d99b6e4 Fix French decimal separator handling in money input fields
Number.parseFloat() only recognizes dots as decimal separators, causing
inputs like "256,54" (French locale) to be parsed as "256". Added a
parseLocaleFloat utility that detects whether comma or dot is the decimal
separator and normalizes accordingly before parsing.

Fixes #1138

https://claude.ai/code/session_01ThfszjiCmbDDPyb4TZqk2X
2026-03-07 10:17:27 +00:00

18 lines
731 B
JavaScript

// Parses a float from a string that may use either commas or dots as decimal separators.
// Handles formats like "1,234.56" (English) and "1.234,56" (French/European).
export default function parseLocaleFloat(value) {
if (typeof value !== "string") return Number.parseFloat(value) || 0
const cleaned = value.replace(/\s/g, "")
const lastComma = cleaned.lastIndexOf(",")
const lastDot = cleaned.lastIndexOf(".")
if (lastComma > lastDot) {
// Comma is the decimal separator (e.g., "1.234,56" or "256,54")
return Number.parseFloat(cleaned.replace(/\./g, "").replace(",", ".")) || 0
}
// Dot is the decimal separator (e.g., "1,234.56" or "256.54")
return Number.parseFloat(cleaned.replace(/,/g, "")) || 0
}