I have a string value of 41,123 and I want to convert it to an integer in JavaScript.
I tried parseInt(41,123, 10) and parseFloat, but nothing gives me correct answer.
ParseInt, parseFloat work well until ma is encountered, but to above the result is '41'.
Does anyone have an idea about the fix?
I have a string value of 41,123 and I want to convert it to an integer in JavaScript.
I tried parseInt(41,123, 10) and parseFloat, but nothing gives me correct answer.
ParseInt, parseFloat work well until ma is encountered, but to above the result is '41'.
Does anyone have an idea about the fix?
Share Improve this question edited Dec 16, 2014 at 22:19 rgettman 178k30 gold badges279 silver badges361 bronze badges asked Jun 13, 2012 at 8:51 RMNRMN 7529 gold badges25 silver badges46 bronze badges 2- parseInt("41,123", 10) = 41, what's wrong with this result? – Fabrizio Calderan Commented Jun 13, 2012 at 8:51
- Yes, but I think it should be 41123. The ma is a notation for thousands. – beeglebug Commented Jun 13, 2012 at 8:52
3 Answers
Reset to default 6var myInt = parseInt("41,123,10".replace(/,/g,""));
Here is the solution:
Number(s.replace(/,/g, ""))
Regex is needed in replacement to remove all ma characters, otherwise only one ma will be replaced.
You can remove the ma, and then parse; let's say the number is variable s
:
parseInt(s.replace(/,/g, '')