Skip to content
instance method

Float#ceil

() -> ::Integer
(::int digits) -> (::Integer | ::Float)
ceil(ndigits = 0) → float or integer

Returns a numeric that is a “ceiling” value for self, as specified by the given ndigits, which must be an integer-convertible object.

When ndigits is positive, returns a Float with ndigits decimal digits after the decimal point (as available, but no fewer than 1):

Example
f = 12345.6789
f.ceil(1) # => 12345.7
f.ceil(3) # => 12345.679
f.ceil(30) # => 12345.6789
f = -12345.6789
f.ceil(1) # => -12345.6
f.ceil(3) # => -12345.678
f.ceil(30) # => -12345.6789
f = 0.0
f.ceil(1)   # => 0.0
f.ceil(100) # => 0.0

When ndigits is non-positive, returns an Integer 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 12345.6789.ceil(ndigits)
0 1 12346
-1 10 12350
-2 100 12400
-3 1000 13000
-4 10000 20000
-5 100000 100000

Examples with negative self:

ndigits Granularity -12345.6789.ceil(ndigits)
0 1 -12345
-1 10 -12340
-2 100 -12300
-3 1000 -12000
-4 10000 -10000
-5 100000 0

When self is zero and ndigits is non-positive, returns Integer zero:

Example
0.0.ceil(0)  # => 0
0.0.ceil(-1) # => 0
0.0.ceil(-2) # => 0

Note that the limited precision of floating-point arithmetic may lead to surprising results:

Example
(2.1 / 0.7).ceil  #=> 4 # Not 3 (because 2.1 / 0.7 # => 3.0000000000000004, not 3.0)

Related: Float#floor.

See also #arg #coerce #angle #denominator also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0