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

How to Parse a String with Comma Thousands Separators in JavaScript?

Patricia Arquette
Release: 2024-12-01 00:33:15
Original
255 people have browsed it

How to Parse a String with Comma Thousands Separators in JavaScript?

Parsing a String with Comma Thousands Separator to a Number

When faced with a string containing a comma thousands separator, such as "2,299.00," parsing it into a number can pose a challenge. Using parseFloat fails due to the presence of the comma.

Solution: Removing the Comma

The simplest and most effective solution is to remove the commas altogether. JavaScript provides the replace() method, which can be used to replace the commas with an empty string (''):

let output = parseFloat("2,299.00".replace(/,/g, ''));
console.log(output);
Copy after login

By replacing each comma with an empty string, we remove the thousands separator and successfully parse the string into the desired number: 2299.

The above is the detailed content of How to Parse a String with Comma Thousands Separators 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template