I have to integer variables a and b.
I would like to compute the percentage a/b in ruby, considering that if both are zero the result should be zero.
With literals its easy, I just need to add .0 in the numbers: 12.0/17.0, with variables that come from other calculations:
# a == 12 and b == 17
a/b => 0
# a == 0 and b == 0
a/b => Infinity
You can use
a.to_f/bso you convert the integer number in a floating number.Another trick you can do is something like
a*1.0/b, but I think the first solution is better.Just check the
a==0 && b==0with anif.