On Tue, Dec 4, 2012 at 9:01 PM, botp <[email protected]> wrote: > On Wed, Dec 5, 2012 at 2:31 AM, Alexander G. <[email protected]> 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/ -- You received this message because you are subscribed to the Google Groups ruby-talk-google group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at https://groups.google.com/d/forum/ruby-talk-google?hl=en
