From: Robert Klemme Date: 2012-10-10T16:19:48+09:00 Subject: Re: Convert String to Currency On Wed, Oct 10, 2012 at 12:16 AM, ideal one wrote: > Hi All, > I have a String and i need to convert it into Currency format and > verify whether it matches to the expected. > Let me explain in detail > > String = "$6,178.50 USD / 22,693.01 AED" > I want to split it into 2 different variables like > > usa_price = $6,178.50 > aed_price = 22,693.01 > > expected_output= $6,178.50 * 3.67 = 22,693.01 (should match value in > AED) > > How do i do this in Ruby!!! If you always expect a division here is one way: irb(main):001:0> s = "$6,178.50 USD / 22,693.01 AED" => "$6,178.50 USD / 22,693.01 AED" irb(main):011:0> %r{\$(\d+(?:,\d+)*(?:\.\d+)?)\s*USD\s*/\s*(\d+(?:,\d+)*(?:\.\d+)?)\s*AED} =~ s and p $1, $2 "6,178.50" "22,693.01" => ["6,178.50", "22,693.01"] Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/