Division Operator
The forward slash (/
) can be used in Ruby as a division operator for dividing two numbers, such as integers or floats.
See the forward slash page for other uses of the /
symbol.
Here are some examples of using /
to divide two integers. Because both operands are integers, the result will be an integer which means some rounding takes place.
> 10 / 2
=> 5
> 10 / 3
=> 3
If we want a more precise result from a division operation, we'll need to make one or both of the operands explicitly a float.
> 10.0 / 3
=> 3.3333333333333335
> 10 / 3.0
=> 3.3333333333333335
> 10.0 / 3.0
=> 3.3333333333333335
> 5 / 2.5
=> 2.0
The other types of numerics like Complex
, Rational
, and BigDecimal
also support the /
operator.