Forum: Ruby convert a string into a decimal

A67ab5ea74d9b3685ddeaf4f5618c82b?d=identicon&s=25 Ernst Tanaka (ernsttanaka)
on 2007-11-06 16:16
I am a new Ruby programmer. I am really amazed with the ease of
programming.

I seem not to be able to find a solution for the following task.

I want to convert an object with the class string into a object with the
class fixnum or decimal. So I can store the result in a Decimal(13,2)
sql field.


The value of the object string is "100,000,000.00" (including the comma
and the decimal point.

Appreciate all the help at for hand!

Ernst
D031782a536596736b44eb6da9b316b8?d=identicon&s=25 Paulo Carvalho (pjmorce)
on 2007-11-06 16:19
   > a="100000000.00"
   > a.to_f
=> 100000000.0


Ernst Tanaka wrote:
> I am a new Ruby programmer. I am really amazed with the ease of
> programming.
>
> I seem not to be able to find a solution for the following task.
>
> I want to convert an object with the class string into a object with the
> class fixnum or decimal. So I can store the result in a Decimal(13,2)
> sql field.
>
>
> The value of the object string is "100,000,000.00" (including the comma
> and the decimal point.
>
> Appreciate all the help at for hand!
>
> Ernst
A67ab5ea74d9b3685ddeaf4f5618c82b?d=identicon&s=25 Ernst Tanaka (ernsttanaka)
on 2007-11-06 16:28
Paulo Carvalho wrote:
>    > a="100000000.00"
>    > a.to_f
> => 100000000.0
>
>
Thank you for the quick reaction.

to_f seems not to be the solution

a = "13,014,530"
a.to_f

>> 13.0


The result I am looking for is
13014530


Thanks again.

Ernst
230aaa9511e64e8c7e18783b7b839c43?d=identicon&s=25 Jose francisco Gonzalez carmona (jfglez)
on 2007-11-06 16:29
Ernst Tanaka wrote:
> I am a new Ruby programmer. I am really amazed with the ease of
> programming.
>
> I seem not to be able to find a solution for the following task.
>
> I want to convert an object with the class string into a object with the
> class fixnum or decimal. So I can store the result in a Decimal(13,2)
> sql field.
>
>
> The value of the object string is "100,000,000.00" (including the comma
> and the decimal point.
>
> Appreciate all the help at for hand!
>
> Ernst

irb(main):005:0> "100,000,000.00".gsub(',','_').to_f
=> 100000000.0
A67ab5ea74d9b3685ddeaf4f5618c82b?d=identicon&s=25 Ernst Tanaka (ernsttanaka)
on 2007-11-06 16:33
> irb(main):005:0> "100,000,000.00".gsub(',','_').to_f
> => 100000000.0


That did the trick! Thanks
8511333289c6bb8732a76eff998cd842?d=identicon&s=25 Pat George (Guest)
on 2007-11-06 16:49
(Received via mailing list)
Ruby newb incoming:

Since his string is "100,000,000.00" to_f won't work:

irb(main):001:0> a = "100,000,000.00"
=> "100,000,000.00"
irb(main):002:0> a.to_f
=> 100.0

Only thing I could think of was to gsub! it first and get rid of the
commas:

irb(main):001:0> a = "100,000,000.00"
=> "100,000,000.00"
irb(main):003:0> a.gsub!(/,/, '')
=> "100000000.00"
irb(main):004:0> a.to_f
=> 100000000.0

Any other way?
655d6e0d3d21759e6894111c61b3b5e8?d=identicon&s=25 Armin Armbruster (swabianeagle)
on 2007-11-06 16:54
Pat George wrote:

> Any other way?

a.delete(',').to_f

Armin
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.