From: Robert Klemme Date: 2012-12-08T20:02:54+09:00 Subject: Re: Extract number (float) from string On Tue, Dec 4, 2012 at 9:01 PM, botp wrote: > On Wed, Dec 5, 2012 at 2:31 AM, Alexander G. wrote: >> >> how to extract exact number from string: >> $5.99 /LB >> need to get 5.99 as float number > > > see ri String#slice > > eg, > >>"$5.99 /LB"[/[\d.]+/].to_f > => 5.99 That's a quite lazy match which will also try to convert "...." into a float. We can do more specific: irb(main):002:0> "$5.99 /LB"[/\d+\.\d+/].to_f => 5.99 Or, more thoroughly: irb(main):004:0> "$5.99 /LB"[/[-+]?\d+(?:\.\d+)?/].to_f => 5.99 But I agree with sto.mar that storing currency values in a float is a bad idea. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/