Forum: Ruby Extract number (float) from string

Posted by Alexander G. (alexander_g)
on 2012-12-04 19:31
Hi Please advice how to extract exact number from string:

$5.99 /LB

I need to get 5.99 as float number

Thank you in advance
Posted by Sam Duncan (Guest)
on 2012-12-04 19:42
(Received via mailing list)
On 12/05/2012 07:31 AM, Alexander G. wrote:
> Hi Please advice how to extract exact number from string:
>
> $5.99 /LB
>
> I need to get 5.99 as float number
>
> Thank you in advance
>
How about;

 > irb
1.9.3p125 :001 > s = '$5.99 /LB'
  => "$5.99 /LB"
1.9.3p125 :002 > s.match(/^\$(\d+\.\d+).*$/)[1].to_f
  => 5.99

You may want to split it into steps and do some error checking, or just
wrap it in a rescue, depending on where your input comes from and where
your output is going.

Sam
Posted by Alexander G. (alexander_g)
on 2012-12-04 20:08
Sam Duncan wrote in post #1087818:
> On 12/05/2012 07:31 AM, Alexander G. wrote:
>> Hi Please advice how to extract exact number from string:
>>
>> $5.99 /LB
>>
>> I need to get 5.99 as float number
>>
>> Thank you in advance
>>
> How about;
>
>  > irb
> 1.9.3p125 :001 > s = '$5.99 /LB'
>   => "$5.99 /LB"
> 1.9.3p125 :002 > s.match(/^\$(\d+\.\d+).*$/)[1].to_f
>   => 5.99
>
> You may want to split it into steps and do some error checking, or just
> wrap it in a rescue, depending on where your input comes from and where
> your output is going.
>
> Sam

Sorry didn't work for me.
That's what it returns: $5.99 /LB
still the same
Posted by 7stud -- (7stud)
on 2012-12-04 20:50
prices = [
  '$ 5.99/LB',
  '$5.99 /LB',
  '5.99 / lb',
  '0.99 / lb',
  '.99/lb'
]


results = prices.map do |str|
  str.match(/[.\d]+/)[0].to_f
end

p results

--output:--
[5.99, 5.99, 5.99, 0.99, 0.99]
Posted by unknown (Guest)
on 2012-12-04 21:01
(Received via mailing list)
Am 04.12.2012 19:31, schrieb Alexander G.:
> Hi Please advice how to extract exact number from string:
>
> $5.99 /LB
>
> I need to get 5.99 as float number
>
> Thank you in advance
>

Using float for currency is a bad idea.

[13] pry(main)> 1.11 - 0.12
=> 0.9900000000000001
Posted by botp (Guest)
on 2012-12-04 21:02
(Received via mailing list)
On Wed, Dec 5, 2012 at 2:31 AM, Alexander G. <lists@ruby-forum.com> 
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

best regards -botp
Posted by unknown (Guest)
on 2012-12-04 21:14
(Received via mailing list)
Am 04.12.2012 21:00, schrieb sto.mar@web.de:
> Using float for currency is a bad idea.
>
> [13] pry(main)> 1.11 - 0.12
> => 0.9900000000000001

/\$(?<dollars>\d+)\.(?<cents>\d+)/ =~ "$5.99 /LB"
amount_in_cent = 100 * dollars.to_i + cents.to_i  # => 599

(see RegExp documentation)
Posted by tamouse mailing lists (Guest)
on 2012-12-06 02:44
(Received via mailing list)
On Tue, Dec 4, 2012 at 1:50 PM, 7stud -- <lists@ruby-forum.com> wrote:
>   str.match(/[.\d]+/)[0].to_f

Interestingly, this works because of the way to_f interprets such things 
as:

".......".to_f # => 0.0
".0.0.0.0.0.".to_f # => 0.0

both of which will also be caught by the regexp.
Posted by Joanne Daudier (Guest)
on 2012-12-06 02:54
(Received via mailing list)
I can't figure out how to unsubscribe.  Help!

Joanne
Posted by Robert Klemme (robert_k78)
on 2012-12-08 12:04
(Received via mailing list)
On Tue, Dec 4, 2012 at 9:01 PM, botp <botpena@gmail.com> wrote:
>
>>"$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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.