End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Float#ceil
ceil(ndigits = 0) → float or integer
numeric.c
View on GitHub ↗
static VALUE
flo_ceil(int argc, VALUE *argv, VALUE num)
{
int ndigits = flo_ndigits(argc, argv);
return rb_float_ceil(num, ndigits);
}
Returns the smallest number greater 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.ceil(1) # => 12345.7 f.ceil(3) # => 12345.679 f = -12345.6789 f.ceil(1) # => -12345.6 f.ceil(3) # => -12345.678
When ndigits is non-positive, returns an integer with at least ndigits.abs trailing zeros:
Example
f = 12345.6789 f.ceil(0) # => 12346 f.ceil(-3) # => 13000 f = -12345.6789 f.ceil(0) # => -12345 f.ceil(-3) # => -12000
Note that the limited precision of floating-point arithmetic may lead to surprising results:
Example
(2.1 / 0.7).ceil #=> 4 (!)
Related: Float#floor.