Is posible String#to_s doesn't add the decimal value if not needed?

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

Hi, I want to convert a string to a Float value:

irb> "1.5".to_f
=3D> 1.5

But if I do:

irb> "1".to_f
=3D> 1.0

I get '1.0' instead of just '1'. Is not possible to just get '1' in this ca=
se?

Thanks a lot.


=2D-=20
I=C3=B1aki Baz Castillo
 
J

Joel VanderWerf

Iñaki Baz Castillo said:
Hi, I want to convert a string to a Float value:

irb> "1.5".to_f
=> 1.5

But if I do:

irb> "1".to_f
=> 1.0

I get '1.0' instead of just '1'. Is not possible to just get '1' in this case?

Well, you could do this:

a = ["1", "1.5"].map do |x|
Float x # validate
eval x
end

p a
 
J

Joel VanderWerf

Just a thought....

Numeric("1.5").should_return(1.5)
Numeric("1").should_return(1)
 
I

Iñaki Baz Castillo

El Mi=C3=A9rcoles, 2 de Julio de 2008, Joel VanderWerf escribi=C3=B3:
Just a thought....

Numeric("1.5").should_return(1.5)
Numeric("1").should_return(1)

Thanks to all for your fast replies :)

=2D-=20
I=C3=B1aki Baz Castillo
 

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

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,823
Latest member
Nadia88

Latest Threads

Top