function toFloat(localFloatStr) {
// Count dots and commas
const commaCount = (localFloatStr.match(/,/g) || []).length;
const dotCount = (localFloatStr.match(/\./g) || []).length;
let normalized = localFloatStr;
if (commaCount && dotCount) {
// Assume the separator closest to the right is the decimal
if (localFloatStr.lastIndexOf(',') > localFloatStr.lastIndexOf('.')) {
normalized = localFloatStr.replace(/\./g, '').replace(',', '.');
} else {
normalized = localFloatStr.replace(/,/g, '');
}
} else if (commaCount) {
// Only commas — assume commas are decimal
normalized = localFloatStr.replace(/\./g, '').replace(',', '.');
} else {
// Only dots — assume dots are decimal
normalized = localFloatStr.replace(/,/g, '');
}
return parseFloat(normalized);
}
console.log(toFloat('1,223,455.678'));
console.log(toFloat('1.223.455,678'));
console.log(toFloat('2.453,78'));
console.log(toFloat('2,453.78'));
/*
run:
1223455.678
1223455.678
2453.78
2453.78
*/