97

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 #=> 1112
Nic
  • 6,211
  • 10
  • 46
  • 69
mCY
  • 2,731
  • 7
  • 25
  • 43

7 Answers7

183

How about this?

 "1,112".delete(',').to_i
Michael Kohl
  • 66,324
  • 14
  • 138
  • 158
  • Thanks for your answer. It's way much better than mine. In fact i was hoping there would be one call instead of two. like: "1,112".to_money. After reviewing the answers, it's likely there's no such method. Anyway, thanks so much! – mCY Jul 16 '12 at 08:55
  • @michealKohi can you tell me Why delete() is much faster than doing the same in gsub which accepts regex, I have always thought regex way is faster. I have ran Benchmark on both on its a huge difference in execution time. – Abhinay Aug 29 '14 at 11:15
  • 1
    @Abhinay Regarding your bench marks: sample size? String size? – Michael Kohl Sep 19 '14 at 18:36
6

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.

Yì Yáng
  • 101
  • 1
  • 6
4

Some more convenient

"1,1200.00".gsub(/[^0-9]/,'') 

it makes "1 200 200" work properly aswell

  • 8
    Even better is `'1,1200.00'.gsub(/[^0-9\.]/,'').to_f`. – Hauleth Jul 13 '12 at 08:49
  • 7
    Wait, why would you want to convert `"1,1200.00"` to `"1120000"`? Don't you just want to replace the `,` (or, depending on the locale, the `.`)? Also, this doesn't convert it to an integer or float, just a string without delimiters. – brymck Jul 13 '12 at 08:53
  • 3
    @Bryan: To venture a guess, you might want to do this in a banking application, when converting a currency amount that the user has input, in order to deal with only integers inside the application: for instance, converting dollars and cents to cents. This method will avoid any rounding errors that might crop up unexpectedly when dealing with floating-point numbers. When a currency amount needs to be shown to the user, the cent amount can be converted to dollars and cents. (Though that is not what the questioner asked about.) – Teemu Leisti Aug 06 '13 at 13:35
  • 3
    This answer doesn't even address the asker's request to convert the string to an integer or float. – Mr. Lance E Sloan Sep 10 '13 at 14:18
2

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
Nick Dickinson-Wilde
  • 1,015
  • 2
  • 15
  • 21
Mahesh
  • 21
  • 1
1

I would do using String#tr :

"1,112".tr(',','').to_i # => 1112
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
1

If someone is looking to sub out more than a comma I'm a fan of:

"1,200".chars.grep(/\d/).join.to_i

dunno about performance but, it is more flexible than a gsub, ie:

"1-200".chars.grep(/\d/).join.to_i
davidpm4
  • 562
  • 1
  • 7
  • 22
-1
String count = count.replace(",", "");
Ehsan
  • 12,072
  • 2
  • 20
  • 33