Convert String to Currency

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!!!

···

--
Posted via http://www.ruby-forum.com/.

str = "$6,178.50 USD / 22,693.01 AED"

regex = /
  \d
  [\d,.]*
/xms

str.scan(regex) do |match|
  puts match.gsub(',', '').to_f + 1
end

--output:--
6179.5
22694.01

···

--
Posted via http://www.ruby-forum.com/.

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

···

On Wed, Oct 10, 2012 at 12:16 AM, ideal one <lists@ruby-forum.com> 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!!!

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

s = "$6,178.50 USD / 22,693.01 AED"
delta = 100.0 #only good to 3 sig figs due to 3.75
h = Hash[*s.delete($,/).split()].invert
h.each {|k, v| h[k] = v.to_f}
puts h["US"]
puts h["AED"]
puts (h["US"] * 3.67).between?( h["AED"] - delta, h["AED"] + delta ) )

6178.5
22693.01
true

Todd

^^ my version:
s="$6,178.50 USD / 22,693.01 AED"
x=s.gsub(',','').split(/[ a-zA-Z\$\/]+/).reject{|x| x.empty?}

# x is ["6178.50", "22693.01"]

···

On Wed 10 Oct 2012 02:19:48 PM WIT, Robert Klemme wrote:

On Wed, Oct 10, 2012 at 12:16 AM, ideal one <lists@ruby-forum.com> 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

Sorry, supposed to be "USD", not "US"

···

On Wed, Oct 10, 2012 at 7:35 AM, Todd Benson <caduceass@gmail.com> wrote:

s = "$6,178.50 USD / 22,693.01 AED"
delta = 100.0 #only good to 3 sig figs due to 3.75
h = Hash[*s.delete($,/).split()].invert
h.each {|k, v| h[k] = v.to_f}
puts h["US"]
puts h["AED"]
puts (h["US"] * 3.67).between?( h["AED"] - delta, h["AED"] + delta ) )

6178.5
22693.01
true