ceil
.. ceil([T,] x, [digits, [base]])
``ceil(x)`` returns the nearest integral value of the same type as ``x``
that is greater than or equal to ``x``.
``ceil(T, x)`` converts the result to type ``T``, throwing an
``InexactError`` if the value is not representable.
``digits`` and ``base`` work as for :func:`round`.
Examples
In the Julia programming language, the function ceil
is used to round a number up to the nearest integral value.
-
Round up a floating-point number:
julia> ceil(3.7) 4.0
This example rounds up the floating-point number 3.7 to 4.0.
-
Round up a negative number:
julia> ceil(-2.5) -2.0
It rounds up the negative number -2.5 to -2.0.
-
Specify the number of digits:
julia> ceil(3.14159, digits=3) 3.142
By setting the
digits
argument to 3, it rounds up to the nearest number with three decimal places. -
Specify the base for rounding:
julia> ceil(17, base=5) 20.0
Here, the number 17 is rounded up to the nearest multiple of 5.
- Convert the result to a specific type:
julia> ceil(Int, 2.5) 3
By providing the type
Int
as the first argument, it returns the rounded-up value as an integer.
Common mistake example:
julia> ceil("2.5")
ERROR: MethodError: no method matching ceil(::String)
In this example, the function ceil
is applied to a string instead of a numeric value. The ceil
function can only be used with numbers, so it's important to pass a valid numeric argument to avoid such errors.
See Also
abs2, beta, binomial, ceil, cell, cross, ctranspose, ctranspose!, cummin, cumprod, cumprod!, cumsum, cumsum!, cumsum_kbn, div, divrem, eigfact, eigfact!, eigmin, eps, erf, erfc, erfcinv, erfcx, erfi, erfinv, exp, exp10, exp2, expm1, exponent, factor, factorial, factorize, floor, gcd, invmod, log, log10, log1p, log2, logspace, max, min, mod, mod1, modf, next, nextpow, nextprod, num, primes, primesmask, prod, realmin, sqrt, sum!, sumabs, sumabs!, sumabs2, sumabs2!,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.