Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string with comma to integer

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 
like image 524
mCY Avatar asked Jul 13 '12 08:07

mCY


People also ask

How do you convert a string to a number with a comma?

To parse a string with commas to a number:Use the replace() method to remove all the commas from the string. The replace method will return a new string containing no commas. Convert the string to a number.

How do you convert a string separated by a comma to an array?

Use the String. split() method to convert a comma separated string to an array, e.g. const arr = str. split(',') . The split() method will split the string on each occurrence of a comma and will return an array containing the results.


1 Answers

How about this?

 "1,112".delete(',').to_i 
like image 173
Michael Kohl Avatar answered Sep 28 '22 09:09

Michael Kohl