-3

So I'm trying to do some ill-advised on-page ERB to calculate the value for a progress bar.

I have @total_completed_lessons and @total_course_lessons (which I know don't need @ signs, but I'm still troubleshooting...). These populate correctly.

However, when I try to divide them it comes out to zero: @total_completed_lessons / @total_course_lessons

What is going on? How is math not math-ing?

Here's a screenshot to show how I'm doing things. (The 'd' is just to get the error screen to pop up so I can play with variables real-time.)

enter image description here

Liz
  • 1,369
  • 2
  • 26
  • 61

1 Answers1

2

It results in zero because you have to provide floats. This should work:

@total_course_lessons = @course_lesson_ids.count.to_f
@total_completed_lessons = @completed_lessons.count.to_f
percent = @total_completed_lessons / @total_course_lessons * 100
Spinshot
  • 275
  • 2
  • 12
  • 2
    Could be simplified with just...`percent = @total_completed_lessons / @total_course_lessons.to_f * 100`...typically the denominator is the only value that is converted to a float in order to avoid truncation – Mark Merritt Sep 23 '20 at 23:13
  • Thank you! I didn't see that it was rounding to 0 as opposed to just printing 0. Now that I remember about integers vs floats this makes sense! – Liz Sep 24 '20 at 17:31