What is float.modulo() in Ruby?

Overview

The modulo() method in Ruby is used to return the modulo after dividing a float value by another.

Note: The modulo is the remainder after dividing one number by another number.

Syntax

float_value.modulo(other)

Parameters

  • float_value: This float value invokes the modulo() method. It is the dividend.
  • other: This is the divisor. It is another float value that divides the float_value. When the division is done, the modulo() method then returns the modulo of the division.

Return value

The value returned is the modulo after the division of float_value by other.

Code example

# create some float values
f1 = 3.5555
f2 = 45.00
f3 = -0.999
f4 = 200.12
# invoke the modulo() method
# and print results
puts f1.modulo(2.3)
puts f2.modulo(1.0)
puts f3.modulo(-2.1)
puts f4.modulo(100.12)

Explanation

  • Lines 2-5: Float values f1, f2, f3 and f4 were created.
  • Lines 9-12: The modulo() method was invoked on the float values we created, which took some other float values, which are the divisors. Then the results were printed to the console.

Free Resources