5

I am able to convert a number to comma separated locale string. Is there a similar function to convert comma separated locale string to a number?

var number = 1000000;
number.toLocaleString();
Output: "1,000,000"

How to convert 1,000,000 to 1000000 using JavaScript?

31piy
  • 23,323
  • 6
  • 47
  • 67
Bharadwaj
  • 135
  • 1
  • 1
  • 7

1 Answers1

-3

var number = 1000000;
console.log('numeric number', number); //1000000

var str = number.toLocaleString();  
console.log('with commas', str);  //1,000,000

var num = str.replace(/\D/g,'');
console.log('string without commas', num);  //1000000  (string)

var num2 = parseInt(num);
console.log('numeric', num2);  //1000000  (numeric)

An easy way to convert a string that might contain commas to a number is to find any non-digits within the string and remove it, thus, remaining with only digits.

var num = number.toLocaleString().replace(/\D/g,''); //1000000
Ahmad
  • 12,336
  • 6
  • 48
  • 88