From number to currency string is easy through Number.prototype.toLocaleString. However the reverse seems to be a common problem. The thousands separator and decimal point may not be obtained in the JS standard.
In this particular question the thousands separator is a white space " "
but in many cases it can be a period "."
and decimal point can be a comma ","
. Such as in 1 000 000,00
or 1.000.000,00
. Then this is how i convert it into a proper floating point number.
var price = "1 000.000,99",
value = +price.replace(/(\.|\s)|(\,)/g,(m,p1,p2) => p1 ? "" : ".");
console.log(value);
_x000D_
So the replacer callback takes "1.000.000,00"
and converts it into "1000000.00"
. After that +
in the front of the resulting string coerces it into a number.
This function is actually quite handy. For instance if you replace the p1 = ""
part with p1 = ","
in the callback function, an input of 1.000.000,00
would result 1,000,000.00