Method: Integer#ceil

Defined in:
numeric.c

#ceil(*args) ⇒ Object

:markup: markdown

call-seq:

ceil(ndigits = 0) -> integer

Returns an integer that is a “ceiling” value for ‘self`, as specified by the given `ndigits`, which must be an [integer-convertible object](implicit_conversion.rdoc@Integer-Convertible+Objects).

  • When ‘self` is zero, returns zero (regardless of the value of `ndigits`):

    ```
    0.ceil(2)  # => 0
    0.ceil(-2) # => 0
    ```
    
  • When ‘self` is non-zero and `ndigits` is non-negative, returns `self`:

    ```
    555.ceil     # => 555
    555.ceil(50) # => 555
    ```
    
  • When ‘self` is non-zero and `ndigits` is negative, returns a value based on a computed granularity:

    - The granularity is `10 ** ndigits.abs`.
    - The returned value is the smallest multiple of the granularity
      that is greater than or equal to `self`.
    
    Examples with positive `self`:
    
    | ndigits | Granularity | 1234.ceil(ndigits) |
    |--------:|------------:|-------------------:|
    | -1      | 10          | 1240               |
    | -2      | 100         | 1300               |
    | -3      | 1000        | 2000               |
    | -4      | 10000       | 10000              |
    | -5      | 100000      | 100000             |
    
    Examples with negative `self`:
    
    | ndigits | Granularity | -1234.ceil(ndigits) |
    |--------:|------------:|--------------------:|
    | -1      | 10          | -1230               |
    | -2      | 100         | -1200               |
    | -3      | 1000        | -1000               |
    | -4      | 10000       | 0                   |
    | -5      | 100000      | 0                   |
    

Related: Integer#floor.



5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
# File 'numeric.c', line 5920

static VALUE
int_ceil(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_ceil(num, ndigits);
}