[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

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

Iñaki Baz Castillo

7/1/2008 10:09:00 PM

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

4 Answers

ara.t.howard

7/1/2008 10:14:00 PM

0


On Jul 1, 2008, at 4:09 PM, I=F1aki Baz Castillo wrote:

> 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 =20=

> this case?
>
> Thanks a lot.
>
>
> --=20
> I=F1aki Baz Castillo
>


n =3D Integer(n) rescue Float(n)

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being =20
better. simply reflect on that.
h.h. the 14th dalai lama




Joel VanderWerf

7/1/2008 10:15:00 PM

0

Iñaki Baz Castillo wrote:
> 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

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

Joel VanderWerf

7/1/2008 10:21:00 PM

0

Just a thought....

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

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

Iñaki Baz Castillo

7/1/2008 10:42:00 PM

0

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