End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
Float#ceil
float.ceil([ndigits]) → integer or float
numeric.c
View on GitHub ↗
static VALUE
flo_ceil(int argc, VALUE *argv, VALUE num)
{
int ndigits = 0;
if (rb_check_arity(argc, 0, 1)) {
ndigits = NUM2INT(argv[0]);
}
return rb_float_ceil(num, ndigits);
}
Returns the smallest number greater 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.ceil #=> 2 2.0.ceil #=> 2 (-1.2).ceil #=> -1 (-2.0).ceil #=> -2 1.234567.ceil(2) #=> 1.24 1.234567.ceil(3) #=> 1.235 1.234567.ceil(4) #=> 1.2346 1.234567.ceil(5) #=> 1.23457 34567.89.ceil(-5) #=> 100000 34567.89.ceil(-4) #=> 40000 34567.89.ceil(-3) #=> 35000 34567.89.ceil(-2) #=> 34600 34567.89.ceil(-1) #=> 34570 34567.89.ceil(0) #=> 34568 34567.89.ceil(1) #=> 34567.9 34567.89.ceil(2) #=> 34567.89 34567.89.ceil(3) #=> 34567.89
Note that the limited precision of floating point arithmetic might lead to surprising results:
Example
(2.1 / 0.7).ceil #=> 4 (!)