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 via http://www.ruby-forum.com/.
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 via http://www.ruby-forum.com/.
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
On 12/05/2012 07:31 AM, Alexander G. wrote:
prices = [
'$ 5.99/LB',
'$5.99 /LB',
'5.99 / lb',
]
results = prices.map do |str|
str.match(/[.\d]+/)[0].to_f
end
p results
--output:--
[5.99, 5.99, 5.99]
--
Posted via http://www.ruby-forum.com/.
Using float for currency is a bad idea.
[13] pry(main)> 1.11 - 0.12
=> 0.9900000000000001
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
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
On Wed, Dec 5, 2012 at 2:31 AM, Alexander G. <lists@ruby-forum.com> wrote:
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.99You 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 via http://www.ruby-forum.com/\.
/\$(?<dollars>\d+)\.(?<cents>\d+)/ =~ "$5.99 /LB"
amount_in_cent = 100 * dollars.to_i + cents.to_i # => 599
(see RegExp documentation)
Am 04.12.2012 21:00, schrieb sto.mar@web.de:
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
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.
On Tue, Dec 4, 2012 at 1:50 PM, 7stud -- <lists@ruby-forum.com> wrote:
str.match(/[.\d]+/)[0].to_f
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
On Tue, Dec 4, 2012 at 9:01 PM, botp <botpena@gmail.com> wrote:
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 numbersee ri String#slice
eg,
"$5.99 /LB"[/[\d.]+/].to_f
=> 5.99
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
I can't figure out how to unsubscribe. Help!
Joanne
Date: Thu, 6 Dec 2012 10:44:12 +0900
From: tamouse.lists@gmail.com
Subject: Re: Extract number (float) from string
To: ruby-talk@ruby-lang.orgOn Tue, Dec 4, 2012 at 1:50 PM, 7stud -- <lists@ruby-forum.com> wrote:
> str.match(/[.\d]+/)[0].to_fInterestingly, this works because of the way to_f interprets such things as:
".......".to_f # => 0.0
".0.0.0.0.0.".to_f # => 0.0both of which will also be caught by the regexp.