3

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 comma is encountered, but to above the result is '41'.

Does anyone have an idea about the fix?

4

3 回答 3

6
var myInt = parseInt("41,123,10".replace(/,/g,""));
于 2012-06-13T08:52:04.663 回答
2

这是解决方案:

Number(s.replace(/,/g, ""))

替换时需要使用正则表达式来删除所有逗号字符,否则只会替换一个逗号。

于 2012-06-13T08:54:05.047 回答
1

可以去掉逗号,然后解析;假设数字是可变的s

parseInt(s.replace(/,/g, '')
于 2012-06-13T08:55:27.207 回答