I guess you could call this a noobie problem as well. I''m trying to divide two numbers by each other... For example: <% for element in project.elements %> Element: <%= horizontal_bar_graph [element.name, ((element.completed_hours / element.hours) * 100)] %><br /> <% end %> element.completed_hours == 2 element.hours == 5 (2 / 5) * 100 == 40 However, it returns a total sum of 0. This is incorrect, of course. Why does it do that? I tried it with magic numbers instead of the ''element.whatever'' things, and 2 / 5 still equalled zero. However, 5 / 2 worked, and so did 0.2 / 0.5. Why is this and how do I get it working?? Thanks in advance, Isaac -- Posted via http://www.ruby-forum.com/.
On Mon Jul 17, 2006 at 05:27:56AM +0200, Isaac Rowntree wrote:> I guess you could call this a noobie problem as well. I''m trying to > divide two numbers by each other... > > For example: > > <% for element in project.elements %> > Element: <%= horizontal_bar_graph [element.name, > ((element.completed_hours / element.hours) * 100)] %><br /> > <% end %> > > element.completed_hours == 2 > element.hours == 5 > > (2 / 5) * 100 == 40it equals 0. (2.0 / 5.0) * 100 == 40.0> However, it returns a total sum of 0. This is incorrect, of course. Why > does it do that? > > I tried it with magic numbers instead of the ''element.whatever'' things, > and 2 / 5 still equalled zero. However, 5 / 2 worked, and so did 0.2 / > 0.5. Why is this and how do I get it working?? > > > Thanks in advance, Isaac > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
> (2.0 / 5.0) * 100 == 40.0im not sure how you cast to float. maybe .to_f? in TCL you dont even need the .0, just a . is enough...
> it equals 0. (2.0 / 5.0) * 100 == 40.0Sorry? That made no sense at all... If it equals zero, then how do I make it so that it does what I want? -- Posted via http://www.ruby-forum.com/.
> im not sure how you cast to float. maybe .to_f? in TCL you dont even > need the .0, just a . is enough...Thanks! Please disregard my last post. .to_f worked like a charm :) -- Posted via http://www.ruby-forum.com/.