Currency to number conversion rails

Jorge's answer is good, but I think you'll need to know how the currency is entered. This will require whitelisting more than black listing.

def currency_to_number currency
 currency.to_s.gsub(/[$,]/,'').to_f
end

The number_to_currency formats a number. If you are trying to convert something like $8.000 into a number you might want to create a new method to parse the string into a number. something like:

result = "$8.000".gsub(/[^\d]/, '').to_f

For an improved conversion of something like:

"$8.000,00"

You are going to need a better regexp.


I just tested this in IRB you will want to put this in some sort of method for use with Rails though.

price = "$8,000.90"
price.match(/(\d.+)/)[1].gsub(',','').to_f

#=> 8000.9