On Dec 17, 12:42 pm, Tom Norian <tomnor... / yahoo.com> wrote:
> Hey all...I am hoping for a tip
>
> I have a table I want to lay out dynamcially in rails where if a game
> has 6 periods the first 3 periods will be in the first column and the
> 4-6 in the second column.  If someone picks an odd number of periods ,
> say 5, I want the 1-3 in the first column and 4 and 5 in the second.
>
> I have figured out how to iterate through IF I could get 5/2 to yield me
> 3.
>
> How do I divide a number (integer) by 2 and get a result that is like:
>
> 5/2 I want 3
>  6/2 I want 3
> 7/2 I want 4
>
> I'm thinking perhaps I could test for oddness then add one to the result
> if odd, but that seems kinda a lot of lines for something so simple.
>
> half_periods = 0
> if num_periods%2 == 1
>   half_periods = num_periods/2 +1
> else
>   half_periods = num_periods/2
> end
>
> Is there a better way?
> --
> Posted viahttp://www.ruby-forum.com/.

Use floats and #round.

def div(x, y)
  (x.to_f/y).round
end

div(5,2) # => 3
div(6,2) # => 3
div(7,2) # => 4

Regards,
Jordan