mirror of
https://github.com/we-promise/sure.git
synced 2026-04-08 23:04:49 +00:00
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
18 lines
731 B
JavaScript
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
|
|
}
|