jQuery

How can I parse a string with a comma thousand separator to a number?

This is useful when we need to do calculations based on the user input. For formatting the entered number in front end we add comma as thousand separator. But problem is when we try to parse it.

e.g, if you try to parseInt (111,222) you ll get the value as 111. To solve this issue we have to use regex and reformat the value.

var numberWithComma = "111,222";
var res = parseFloat( numberWithComma.replace(/,/g, ''));
console.log(res); //111222

parse a number with thousand comma separator remove comma from number jquery

Share this article

5 thoughts on “How can I parse a string with a comma thousand separator to a number?”

  1. Mohit

    Locale number parsing still trips our forms. Stripping commas before parseFloat is the reminder I needed under deadline.

  2. Ishita

    I prefer Number(str.replaceAll(‘,’,”)) these days. Same idea, slightly cleaner in modern browsers.

  3. Abhishek

    In India we also see lac formatting sometimes. Curious if you’ve dealt with that on top of comma separators.

  4. Divya

    parseFloat with replace(/,/g,”) has bitten me more times than I admit. Locale-aware parsing is still messy though.

  5. Avery

    Good reminder that display format and numeric value should stay separate in the first place.

Leave a Reply

Your email address will not be published. Required fields are marked *