Skip to content

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

instance method

Float#floor

floor(ndigits = 0) → float or integer

Returns the largest number less than or equal to self with a precision of ndigits decimal digits.

When ndigits is positive, returns a float with ndigits digits after the decimal point (as available):

Example
f = 12345.6789
f.floor(1) # => 12345.6
f.floor(3) # => 12345.678
f = -12345.6789
f.floor(1) # => -12345.7
f.floor(3) # => -12345.679

When ndigits is non-positive, returns an integer with at least ndigits.abs trailing zeros:

Example
f = 12345.6789
f.floor(0)  # => 12345
f.floor(-3) # => 12000
f = -12345.6789
f.floor(0)  # => -12346
f.floor(-3) # => -13000

Note that the limited precision of floating-point arithmetic may lead to surprising results:

Example
(0.3 / 0.1).floor  #=> 2 (!)

Related: Float#ceil.

See also #finite? #hash #fdiv #infinite? also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0