Sometimes, we want to parse a string with a comma thousand separator to a number with JavaScript.
In this article, we’ll look at how to parse a string with a comma thousand separator to a number with JavaScript.
How to parse a string with a comma thousand separator to a number with JavaScript?
To parse a string with a comma thousand separator to a number with JavaScript, we remove all the commas from the string before we parse it.
For instance, we write
const output = parseFloat("2,299.00".replace(/,/g, ""));
console.log(output);
to return the "2,299.00"
string without the commas with replace
.
Then we call parseFloat
to parse the returned string into a floating point number.
Conclusion
To parse a string with a comma thousand separator to a number with JavaScript, we remove all the commas from the string before we parse it.