Ruby string with USD "money" converted to number Ruby string with USD "money" converted to number ruby ruby

Ruby string with USD "money" converted to number


Why not remove all non-digit characters before calling .to_i

Example:

"$7,600".gsub(/\D/,'').to_i

And for a floating point number:

"$7,600.90".gsub(/[^\d\.]/, '').to_f


You can do:

"$100.00".scan(/[.0-9]/).join().to_f

or to_i if they're only dollars


You could use the Money gem

Money.parse("$100") == Money.new(10000, "USD")