I''ve got newbie question. I''m trying to make some math, however I''m having problems with number formating. I''d need to have all my numbers float. Example. @variable = 10/100 >> this gives me 0 @variable = 21/7 >> gives me 3 any suggestions...? Thanks keli
On 9.9.2005, at 10.10, milos kelemen wrote:> I''ve got newbie question. > > I''m trying to make some math, however I''m having problems with > number formating. I''d need to have all my numbers float. > > Example. > > @variable = 10/100 >> this gives me 0 > @variable = 21/7 >> gives me 3Ruby does not do automatic type conversion for numbers. If both terms are fixnums, the result will also be a fixnum. So you either need to cast the numbers to float with to_f method, or make either of the numbers a float: 10.0/100 => 0.1 21/7.0 => 3.0 10.to_f / 100 => 0.1 //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
milos kelemen wrote:> I''ve got newbie question. > > I''m trying to make some math, however I''m having problems with number > formating. I''d need to have all my numbers float. > > Example. > > @variable = 10/100 >> this gives me 0 > @variable = 21/7 >> gives me 3 > > any suggestions...? > > Thanks > > keli > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >A number with a decimal point is turned into a Float object automatically. So you should write: v=10/100.0 --> 0.1 Eszter -- Primalgrasp LLC http://primalgrasp.com
You have to use at least one floating number, something like this: @variable = 10/100.to_f or @variable = 10/100.0 El Viernes, 9 de Septiembre de 2005 09:10, milos kelemen escribió:> I''ve got newbie question. > > I''m trying to make some math, however I''m having problems with number > formating. I''d need to have all my numbers float. > > Example. > > @variable = 10/100 >> this gives me 0 > @variable = 21/7 >> gives me 3 > > any suggestions...? > > Thanks > > keli > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Jesús Sánchez Cuadrado wrote:> You have to use at least one floating number, something like this: > > @variable = 10/100.to_f > or > @variable = 10/100.0 > > El Viernes, 9 de Septiembre de 2005 09:10, milos kelemen escribió: > >>I''ve got newbie question. >> >>I''m trying to make some math, however I''m having problems with number >>formating. I''d need to have all my numbers float. >> >>Example. >> >>@variable = 10/100 >> this gives me 0 >>@variable = 21/7 >> gives me 3 >> >>any suggestions...? >> >>Thanks >> >>keli >> >>thanks to all of toy guys...:) _______________________________________________>>Rails mailing list >>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>http://lists.rubyonrails.org/mailman/listinfo/rails