instance method
Integer#floor
(?::int digits) -> ::Integer
floor(ndigits = 0) → integer
numeric.c
View on GitHub ↗
static VALUE
int_floor(int argc, VALUE* argv, VALUE num)
{
int ndigits;
if (!rb_check_arity(argc, 0, 1)) return num;
ndigits = NUM2INT(argv[0]);
if (ndigits >= 0) {
return num;
}
return rb_int_floor(num, ndigits);
}
Returns an integer that is a “floor” value for self, as specified by the given ndigits, which must be an integer-convertible object.
-
When
selfis zero, returns zero (regardless of the value ofndigits):Example0.floor(2) # => 0 0.floor(-2) # => 0
-
When
selfis non-zero andndigitsis non-negative, returnsself:Example555.floor # => 555 555.floor(50) # => 555
-
When
selfis non-zero andndigitsis negative, returns a value based on a computed granularity:-
The granularity is
10 ** ndigits.abs. -
The returned value is the largest multiple of the granularity that is less than or equal to
self.
Examples with positive
self:ndigits Granularity 1234.floor(ndigits) -1 10 1230 -2 100 1200 -3 1000 1000 -4 10000 0 -5 100000 0 Examples with negative
self:ndigits Granularity -1234.floor(ndigits) -1 10 -1240 -2 100 -1300 -3 1000 -2000 -4 10000 -10000 -5 100000 -100000 -
Related: Integer#ceil.