convert a string into a decimal

E

Ernst Tanaka

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
 
E

Ernst Tanaka

Paulo said:
=> 100000000.0
Thank you for the quick reaction.

to_f seems not to be the solution

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


The result I am looking for is
13014530


Thanks again.

Ernst
 
J

Jose francisco Gonzalez carmona

Ernst said:
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
 
P

Pat George

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top