Convert string with comma to integer

后端 未结 7 1785
我寻月下人不归
我寻月下人不归 2020-12-25 09:26

Is there any neat method to convert \"1,112\" to integer 1112, instead of 1?

I\'ve got one, but not neat:

\"1,112\".split(\',\').join.to_i #=> 11         


        
7条回答
  •  遥遥无期
    2020-12-25 09:52

    The following is another method that will work, although as with some of the other methods it will strip decimal places.

    a = 1,112
    b = a.scan(/\d+/).join().to_i => 1112
    

提交回复
热议问题