[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

convert a string into a decimal

Ernst Tanaka

11/6/2007 3:16:00 PM

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
--
Posted via http://www.ruby-....

6 Answers

Paulo Carvalho

11/6/2007 3:20:00 PM

0

> 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

--
Posted via http://www.ruby-....

Ernst Tanaka

11/6/2007 3:29:00 PM

0

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
--
Posted via http://www.ruby-....

Jose francisco Gonzalez carmona

11/6/2007 3:31:00 PM

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

irb(main):005:0> "100,000,000.00".gsub(',','_').to_f
=> 100000000.0


--
Posted via http://www.ruby-....

Pat George

11/6/2007 3:32:00 PM

0

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?

Paulo Carvalho wrote:
> > 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
>>
>
>


Ernst Tanaka

11/6/2007 3:34:00 PM

0


> irb(main):005:0> "100,000,000.00".gsub(',','_').to_f
> => 100000000.0


That did the trick! Thanks
--
Posted via http://www.ruby-....

Armin Armbruster

11/6/2007 3:55:00 PM

0

Pat George wrote:

> Any other way?

a.delete(',').to_f

Armin
--
Posted via http://www.ruby-....