Mega Code Archive

 
Categories / Ruby / Language Basics
 

Division and Truncation

# When you do integer division in Ruby, any fractional part in the result will be truncated. puts 24 / 2 # no problem puts 25 / 2 # uh-oh, truncation puts 25.0 / 2 # using a float as at least one operand solves it puts 25.0 / 2.0 # same when both operands are floats