How to convert 1,000 to 1000 using JavaScript.
console.log(parseInt(1,000));
is taking it as 1
How to convert 1,000 to 1000 using JavaScript.
console.log(parseInt(1,000));
is taking it as 1
You should replace the "," and then doo the parseInt
parseInt("1,000".replace(/,/g,""));
You can use a regular expression to replace all non-digits except for -
and .
when passing the argument to parseInt
:
function parseMyInt(str) {
return parseInt(str.replace(/[^-\.\d]/g,''));
}
console.log(parseMyInt("1,000"));
console.log(parseMyInt("1,000,000"));
console.log(parseMyInt("1,000.1234"));
console.log(parseMyInt("-1,000"));
Expanded the regex to account for negative numbers and decimals.
You need to replace comma with empty character. Something like this below:
parseInt("1,000".replace(/,/g, ""))
Try this,
var num = parseInt("1,000".replace(/\,/g, ''), 10);
As, we need to remove "comma" from the string. We also need "10" as radix as the second parameter.
Thanks