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

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

  > "1.5".to_f
  => 1.5

But if I do:

  > "1".to_f
  => 1.0

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

Thanks a lot.

···

--
Iñaki Baz Castillo

n = Integer(n) rescue Float(n)

a @ http://codeforpeople.com/

···

On Jul 1, 2008, at 4:09 PM, Iñaki Baz Castillo wrote:

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

> "1.5".to_f
=> 1.5

But if I do:

> "1".to_f
=> 1.0

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

Thanks a lot.

--
Iñaki Baz Castillo

--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama

Iñaki Baz Castillo wrote:

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

  > "1.5".to_f
  => 1.5

But if I do:

  > "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

···

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Just a thought....

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

···

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Thanks to all for your fast replies :slight_smile:

···

El Miércoles, 2 de Julio de 2008, Joel VanderWerf escribió:

Just a thought....

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

--
Iñaki Baz Castillo