Division and Truncation : Math Operators « Language Basics « Ruby






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

 








Related examples in the same category

1.get acquainted with math operations in Ruby
2.Using Math Operators
3.A few of Ruby's assignment operators
4.Some of the operators have assignment shortcuts
5.unary operators, + and -, which indicate negative and positive numbers
6.methods such as div, modulo, divmod, quo, and remainder.
7.be careful when you use the div method
8.Abbreviated Assignment Operators
9.note Ruby does not have ++