Ruby string for int / float conversion

So let's say I have a line, '$3,444.11' . How to convert this to float 3,444.11 ? I have a form field. The user can insert "3,444.11" , "3444.11" or "$3,444.11 ” or "€3,444.11" . I need 3,444.11 as a float. Should I resort to regex? Or is there a function 'with a view?

+4
source share
1 answer

I don’t think there is anything like that in the standard library for you. Here's a quick one-liner:

 '$3,444.11'.gsub(/[^\d\.]/, '').to_f # => 3444.11 

However, you can take a look at the money gem for advanced currency string processing.

+14
source

All Articles