Alex Rivera | Logout

How to find remainder of a division in Ruby?

Asked 2012-01-31T20:06:00.813
56

I'm trying to get the remainder of a division using Ruby.

Let's say we're trying to divide 208 by 11.

The final should be "18 with a remainder of 10"...what I ultimately need is that 10.

Here's what I've got so far, but it chokes in this use case (saying the remainder is 0).

division = 208.to_f / 11
rounded = (division*10).ceil/10.0
remainder = rounded.round(1).to_s.last.to_i
Edit
Report

1 Answer

79

The modulo operator:

> 208 % 11
=> 10
answered 2012-01-31T20:07:50.640

Your Answer