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
Share this article

Locale number parsing still trips our forms. Stripping commas before parseFloat is the reminder I needed under deadline.
I prefer Number(str.replaceAll(‘,’,”)) these days. Same idea, slightly cleaner in modern browsers.
In India we also see lac formatting sometimes. Curious if you’ve dealt with that on top of comma separators.
parseFloat with replace(/,/g,”) has bitten me more times than I admit. Locale-aware parsing is still messy though.
Good reminder that display format and numeric value should stay separate in the first place.