# Noob devision question

**URL:** <https://www.ruby-forum.com/t/noob-devision-question/99832>\
**Category:** Ruby\
**Created:** [July 25, 2006, 9:06pm UTC](https://www.ruby-forum.com/t/noob-devision-question/99832 "2006-07-25T21:06:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamiethehutt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4bbf92/32.png) [@jamiethehutt](https://www.ruby-forum.com/u/jamiethehutt)\
**Post date:** [July 25, 2006, 9:06pm UTC](https://www.ruby-forum.com/t/noob-devision-question/99832/1 "2006-07-25T21:06:24Z")

</div>

I’m trying to make a percentage progress bar for an XMMS web interface,  
I want to take the total track time and the elapsed time and then make  
a length for the bar from the percentage completed.

something like:

totalbarsize = 180  
timeelapsed = 10  
totaltime = 30

percomplete = 0.0  
barlength = 0.0

percomplete = timeelapsed / totaltime  
barlength = totalbarsize\*percomplete

puts barlength

Now that should give me a barlength of 60 pixels but it gives 0  
instead. Anyone know what I’m doing wrong?

Thanks!

(I’ll forget that I need barlength as an integer rather than a float  
for now :-P)

---

<div class="post-metadata">

**Author:** ![jamiethehutt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4bbf92/32.png) [@jamiethehutt](https://www.ruby-forum.com/u/jamiethehutt)\
**Post date:** [July 25, 2006, 9:13pm UTC](https://www.ruby-forum.com/t/noob-devision-question/99832/2 "2006-07-25T21:13:06Z")

</div>

jamiethehutt wrote:

> percomplete = 0.0  
> Thanks!
> 
> (I’ll forget that I need barlength as an integer rather than a float  
> for now :-P)

timeelapsed and totaltime are Integers. So when you divide them, it does  
integer division:

irb(main):001:0\> 10/30  
=\> 0  
irb(main):002:0\> 10.0 / 30.0  
=\> 0.333333333333333

try

percomplete = timeelapsed.to\_f / totaltime

or

percomplete = timeelapsed / totaltime.to\_f

or

percomplete = timeelapsed.to\_f / totaltime.to\_f

or

totaltime = 30.0  
timeelapsed = 10.0

You get the idea 🙂 Floats for floating point division.

-Justin

---

<div class="post-metadata">

**Author:** ![jamiethehutt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4bbf92/32.png) [@jamiethehutt](https://www.ruby-forum.com/u/jamiethehutt)\
**Post date:** [July 25, 2006, 9:13pm UTC](https://www.ruby-forum.com/t/noob-devision-question/99832/3 "2006-07-25T21:13:18Z")

</div>

Division of 2 integers results in an integer. So, try setting  
timeelapsed to 10.0 instead of 10.

Tim

---

<div class="post-metadata">

**Author:** ![jamiethehutt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4bbf92/32.png) [@jamiethehutt](https://www.ruby-forum.com/u/jamiethehutt)\
**Post date:** [July 25, 2006, 9:16pm UTC](https://www.ruby-forum.com/t/noob-devision-question/99832/4 "2006-07-25T21:16:28Z")

</div>

jamiethehutt wrote:  
…

> puts barlength  
> You need Floats all the way down.  
> The division of Fixnum values ( timeelapsed / totaltime)  
> produces a Fixnum value, which is 0 as 0.333 gets truncated

put a .0 after all the non-Float values to force them to be Float

---

<div class="post-metadata">

**Author:** ![jamiethehutt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4bbf92/32.png) [@jamiethehutt](https://www.ruby-forum.com/u/jamiethehutt)\
**Post date:** [July 25, 2006, 9:19pm UTC](https://www.ruby-forum.com/t/noob-devision-question/99832/5 "2006-07-25T21:19:45Z")

</div>

“jamiethehutt” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> percomplete = 0.0  
> barlength = 0.0
> 
> percomplete = timeelapsed / totaltime

This is an integer-division, and since totaltime is always bigger than  
timeelapsed (in theory 😉 it is always 0.  
You could try defining totaltime as 30.0 initially or write it as  
percomplete = timeelapsed.to\_f / totaltime  
or something.

> barlength = totalbarsize\*percomplete
> 
> puts barlength
> 
> Now that should give me a barlength of 60 pixels but it gives 0  
> instead. Anyone know what I’m doing wrong?
> 
> Thanks!

Tom

---

<div class="post-metadata">

**Author:** ![jamiethehutt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4bbf92/32.png) [@jamiethehutt](https://www.ruby-forum.com/u/jamiethehutt)\
**Post date:** [July 25, 2006, 10:25pm UTC](https://www.ruby-forum.com/t/noob-devision-question/99832/6 "2006-07-25T22:25:27Z")

</div>

jamiethehutt wrote:

> percomplete = 0.0  
> Thanks!
> 
> (I’ll forget that I need barlength as an integer rather than a float  
> for now :-P)

> > 10/30  
> > =\> 0
> 
> > 10\*100/30  
> > =\> 33

---

<div class="post-metadata">

**Author:** ![jamiethehutt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4bbf92/32.png) [@jamiethehutt](https://www.ruby-forum.com/u/jamiethehutt)\
**Post date:** [July 25, 2006, 9:23pm UTC](https://www.ruby-forum.com/t/noob-devision-question/99832/7 "2006-07-25T21:23:09Z")

</div>

ChrisH wrote:

> > barlength = totalbarsize\*percomplete
> > 
> > puts barlength
> 
> You need Floats all the way down.  
> The division of Fixnum values ( timeelapsed / totaltime)  
> produces a Fixnum value, which is 0 as 0.333 gets truncated
> 
> put a .0 after all the non-Float values to force them to be Float

Fogot to say, to get an Integer (or Fixnum id Ruby parlance) just call  
to\_i on the Float value to covert by truncating the value.

cheers

---

<div class="post-metadata">

**Author:** ![jamiethehutt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4bbf92/32.png) [@jamiethehutt](https://www.ruby-forum.com/u/jamiethehutt)\
**Post date:** [July 25, 2006, 11:09pm UTC](https://www.ruby-forum.com/t/noob-devision-question/99832/8 "2006-07-25T23:09:04Z")

</div>

Use quo. For example:

percomplete = timeelapsed.quo(totaltime) =\> 0.333333333333333

Regards, Morton
