I\'ve got a web page that displays decimals in a user\'s localized format, like so:
7.75
7,75
Here's an example for a locale aware number parser:
function parseLocaleNumber(stringNumber) {
var thousandSeparator = (1111).toLocaleString().replace(/1/g, '');
var decimalSeparator = (1.1).toLocaleString().replace(/1/g, '');
return parseFloat(stringNumber
.replace(new RegExp('\\' + thousandSeparator, 'g'), '')
.replace(new RegExp('\\' + decimalSeparator), '.')
);
}
It uses the current locale of the browser to replace thousand and decimal separators.
With a German locale setting
var n = parseLocaleNumber('1.000.045,22');
n
will be equal to 1000045.22
.