Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Float#%

self % other → float

Returns self modulo other as a float.

For float f and real number r, these expressions are equivalent:

Example
f % r
f-r*(f/r).floor
f.divmod(r)[1]

See Numeric#divmod.

Examples:

Example
10.0 % 2              # => 0.0
10.0 % 3              # => 1.0
10.0 % 4              # => 2.0

10.0 % -2             # => 0.0
10.0 % -3             # => -2.0
10.0 % -4             # => -2.0

10.0 % 4.0            # => 2.0
10.0 % Rational(4, 1) # => 2.0

Float#modulo is an alias for Float#%.

See also #* #** also in 4.1 · 4.0 · 3.4 · 3.3 · 3.1 · 3.0