Try Mike Bostock's coercion of the Intl.NumberFormat tool into a parser.
Credit: https://observablehq.com/@mbostock/localized-number-parsing
ES6:
class NumberParser {
constructor(locale) {
const parts = new Intl.NumberFormat(locale).formatToParts(12345.6);
const numerals = [...new Intl.NumberFormat(locale, {useGrouping: false}).format(9876543210)].reverse();
const index = new Map(numerals.map((d, i) => [d, i]));
this._group = new RegExp(`[${parts.find(d => d.type === "group").value}]`, "g");
this._decimal = new RegExp(`[${parts.find(d => d.type === "decimal").value}]`);
this._numeral = new RegExp(`[${numerals.join("")}]`, "g");
this._index = d => index.get(d);
}
parse(string) {
return (string = string.trim()
.replace(this._group, "")
.replace(this._decimal, ".")
.replace(this._numeral, this._index)) ? +string : NaN;
}
}
ES5:
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var NumberParser = (function () {
function NumberParser(locale) {
_classCallCheck(this, NumberParser);
var parts = new Intl.NumberFormat(locale).formatToParts(12345.6);
var numerals = [].concat(_toConsumableArray(new Intl.NumberFormat(locale, { useGrouping: false }).format(9876543210))).reverse();
var index = new Map(numerals.map(function (d, i) {
return [d, i];
}));
this._group = new RegExp("[" + parts.find(function (d) {
return d.type === "group";
}).value + "]", "g");
this._decimal = new RegExp("[" + parts.find(function (d) {
return d.type === "decimal";
}).value + "]");
this._numeral = new RegExp("[" + numerals.join("") + "]", "g");
this._index = function (d) {
return index.get(d);
};
}
_createClass(NumberParser, [{
key: "parse",
value: function parse(string) {
return (string = string.trim().replace(this._group, "").replace(this._decimal, ".").replace(this._numeral, this._index)) ? +string : NaN;
}
}]);
return NumberParser;
})();