Forum: Ruby convert a string into a decimal

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A52782a82c5d8665f001bf52db2b1019?d=identicon&s=25 Ernst T. (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
853e5820f28e97b61cac70181af387c0?d=identicon&s=25 Paulo C. (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
A52782a82c5d8665f001bf52db2b1019?d=identicon&s=25 Ernst T. (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
9cec7cd79aace1818923698c073fb967?d=identicon&s=25 Jose francisco G. (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
A52782a82c5d8665f001bf52db2b1019?d=identicon&s=25 Ernst T. (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
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?
98ce9b50e8ed7d9c7f3e40a2f7f6928f?d=identicon&s=25 Armin A. (swabianeagle)
on 2007-11-06 16:54
Pat George wrote:

> Any other way?

a.delete(',').to_f

Armin
This topic is locked and can not be replied to.