Parsing a String with a Comma Thousand Separator to a Number
When attempting to parse a string containing a comma as a thousand separator into a number, using parseFloat may not yield the expected result, as it treats the comma as a decimal point. To resolve this issue, consider the following solution:
Remove the commas from the string. JavaScript provides a convenient replace() method that allows you to search for a specific pattern (in this case, commas) and replace it with an empty string.
Here's an example:
let input = "2,299.00"; let output = parseFloat(input.replace(/,/g, '')); console.log(output); // Output: 2299
By removing the commas, we convert the string into a valid numeric format that can be parsed using parseFloat.
The above is the detailed content of How to Parse a String with Comma Thousand Separators into a Number in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!