End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Float#floor
float.floor([ndigits]) → integer or float
numeric.c
View on GitHub ↗
static VALUE
flo_floor(int argc, VALUE *argv, VALUE num)
{
int ndigits = 0;
if (rb_check_arity(argc, 0, 1)) {
ndigits = NUM2INT(argv[0]);
}
return rb_float_floor(num, ndigits);
}
Returns the largest number less than or equal to float with a precision of ndigits decimal digits (default: 0).
When the precision is negative, the returned value is an integer with at least ndigits.abs trailing zeros.
Returns a floating point number when ndigits is positive, otherwise returns an integer.
Example
1.2.floor #=> 1 2.0.floor #=> 2 (-1.2).floor #=> -2 (-2.0).floor #=> -2 1.234567.floor(2) #=> 1.23 1.234567.floor(3) #=> 1.234 1.234567.floor(4) #=> 1.2345 1.234567.floor(5) #=> 1.23456 34567.89.floor(-5) #=> 0 34567.89.floor(-4) #=> 30000 34567.89.floor(-3) #=> 34000 34567.89.floor(-2) #=> 34500 34567.89.floor(-1) #=> 34560 34567.89.floor(0) #=> 34567 34567.89.floor(1) #=> 34567.8 34567.89.floor(2) #=> 34567.89 34567.89.floor(3) #=> 34567.89
Note that the limited precision of floating point arithmetic might lead to surprising results:
Example
(0.3 / 0.1).floor #=> 2 (!)