Convert string with comma to integer

后端 未结 7 1737
我寻月下人不归
我寻月下人不归 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:54

    You may also want to make sure that your code localizes correctly, or make sure the users are used to the "international" notation. For example, "1,112" actually means different numbers across different countries. In Germany it means the number a little over one, instead of one thousand and something.

    Corresponding Wikipedia article is at http://en.wikipedia.org/wiki/Decimal_mark. It seems to be poorly written at this time though. For example as a Chinese I'm not sure where does these description about thousand separator in China come from.

提交回复
热议问题