1

I have an array of string numbers like the follow: "123,556","552,255,242","2,601","242","2","4" and I would like to convert them to int numbers but the numbers with the "," I would like to convert from "123,556" to "123556" first. How do I do so ?

Alon
  • 3,734
  • 10
  • 45
  • 64
  • Possible duplicate of this [question](http://stackoverflow.com/questions/832829/numbers-with-commas-in-javascript). Voting to close. – Robin Maben Aug 02 '12 at 09:16

6 Answers6

1
var numbersArray = ["153,32","32,453,23","45,21"];
for (var i = 0; i < numbersArray.length; i++) {
    numbersArray[i] = parseInt(numbersArray[i].replace(',',''));
}
Mihai Matei
  • 24,166
  • 5
  • 32
  • 50
1
var str = "552,255,242";
var numbr = parseInt(str.replace(/\,/g,''), 10);
Marek Musielak
  • 26,832
  • 8
  • 72
  • 80
0

just use split, join (or replace) to remove the , and parseInt afterwards:

var number = "123,456";
number = number.split(',').join('');
number = parseInt(number, 10);
oezi
  • 51,017
  • 10
  • 98
  • 115
  • If you don't pass a parameter to `.join()` it's the same as passing `','` as argument, i.e. the comma is the default separator. Pass an empty string, otherwise you are not modifying the string at all. – Felix Kling Aug 02 '12 at 09:28
  • thanks for the hint felix, thats what i actually wanted to do. – oezi Aug 02 '12 at 09:46
0

You could use .replace method.

"123,556".replace(/,/g, '');
xdazz
  • 158,678
  • 38
  • 247
  • 274
0

Try this: string.replace(',', '');

Danil Speransky
  • 29,891
  • 5
  • 68
  • 79
0

something like

var parseMe(myarray) {
  var out = new Array(myarray.length);
  for (i=0;i<myarray.length;i++){
      var tokens[] = myarray[i].split(",");
      var s = tokens[0] + tokens[1];
      out.push(parseInt(s));
  }
  return out;
}
Moataz Elmasry
  • 2,509
  • 4
  • 27
  • 37