-4

I have a problem convert string 1,200 to integer 1200

i need convert 1,200 to 1200 integer

how to solve my problem using javascript ?

Encang Cutbray
  • 382
  • 1
  • 2
  • 9
  • this solution might solve your problem https://stackoverflow.com/questions/29255843/is-there-a-way-to-reverse-the-formatting-by-intl-numberformat-in-javascript – Ganesh B. Kadam Nov 17 '21 at 03:59
  • Does this answer your question? [In JavaScript / jQuery what is the best way to convert a number with a comma into an integer?](https://stackoverflow.com/questions/4083372/in-javascript-jquery-what-is-the-best-way-to-convert-a-number-with-a-comma-int) – Always Helping Nov 17 '21 at 04:00
  • 2
    [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/a/261593/3082296) – adiga Nov 17 '21 at 04:07

3 Answers3

0

You can split on the comma, rejoin and parse into a number, or remove the commas with a regex and parse into a number

Note that both of these methods allow for more than 1 ',' eg if 1,000, 000 - that's what the g is for in the regex.

const str = '1,200'

const option1 = parseInt(str.split(',').join(''), 10);
const option2 = parseInt(str.replace(/,/g,''), 10);

console.log('option1 - split and join - then parse: ' + option1);
console.log('option2 - replace with regex - then parse: ' + option2);
gavgrif
  • 15,194
  • 2
  • 25
  • 27
-1

To work in node and browser, use replace with regex.

const num = "1,200"
const parsed = Number(num.replace(/,/g, ""))
Steve
  • 4,372
  • 26
  • 37
-3

You can first replace all commas (with String.replaceAll), then parse:

const str = "1,200"
const parsed = +str.replaceAll(',', '')

console.log(parsed)
Spectric
  • 30,714
  • 6
  • 20
  • 43
  • [String.prototype.replaceAll](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll) – danronmoon Nov 17 '21 at 04:01