Home > Web Front-end > JS Tutorial > How to Parse a String with Comma Thousand Separators into a Number in JavaScript?

How to Parse a String with Comma Thousand Separators into a Number in JavaScript?

DDD
Release: 2024-11-25 11:33:16
Original
882 people have browsed it

How to Parse a String with Comma Thousand Separators into a Number in JavaScript?

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
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template