# Division not working!?!?

**URL:** <https://www.ruby-forum.com/t/division-not-working/98638>\
**Category:** Rails\
**Created:** [July 17, 2006, 5:27am UTC](https://www.ruby-forum.com/t/division-not-working/98638 "2006-07-17T05:27:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![isaacrtree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/65b543/32.png) [@isaacrtree](https://www.ruby-forum.com/u/isaacrtree)\
**Post date:** [July 17, 2006, 5:27am UTC](https://www.ruby-forum.com/t/division-not-working/98638/1 "2006-07-17T05:27:56Z")

</div>

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)] %\>  
  
\<% 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

---

<div class="post-metadata">

**Author:** ![isaacrtree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/65b543/32.png) [@isaacrtree](https://www.ruby-forum.com/u/isaacrtree)\
**Post date:** [July 17, 2006, 5:32am UTC](https://www.ruby-forum.com/t/division-not-working/98638/2 "2006-07-17T05:32:03Z")

</div>

On Mon Jul 17, 2006 at 05:27:56AM +0200, Isaac Rowntree wrote:

> element.completed\_hours == 2  
> element.hours == 5
> 
> (2 / 5) \* 100 == 40

it equals 0. (2.0 / 5.0) \* 100 == 40.0

---

<div class="post-metadata">

**Author:** ![isaacrtree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/65b543/32.png) [@isaacrtree](https://www.ruby-forum.com/u/isaacrtree)\
**Post date:** [July 17, 2006, 5:35am UTC](https://www.ruby-forum.com/t/division-not-working/98638/3 "2006-07-17T05:35:26Z")

</div>

> (2.0 / 5.0) \* 100 == 40.0

im not sure how you cast to float. maybe .to\_f? in TCL you dont even  
need the .0, just a . is enough…

---

<div class="post-metadata">

**Author:** ![isaacrtree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/65b543/32.png) [@isaacrtree](https://www.ruby-forum.com/u/isaacrtree)\
**Post date:** [July 17, 2006, 5:35am UTC](https://www.ruby-forum.com/t/division-not-working/98638/4 "2006-07-17T05:35:49Z")

</div>

> it equals 0. (2.0 / 5.0) \* 100 == 40.0

Sorry? That made no sense at all… If it equals zero, then how do I  
make it so that it does what I want?

---

<div class="post-metadata">

**Author:** ![isaacrtree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/65b543/32.png) [@isaacrtree](https://www.ruby-forum.com/u/isaacrtree)\
**Post date:** [July 17, 2006, 5:38am UTC](https://www.ruby-forum.com/t/division-not-working/98638/5 "2006-07-17T05:38:03Z")

</div>

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