The ceil()
function in Swift returns the nearest smallest integer value greater than or equal to a number.
The figure below shows the mathematical representation of the ceil()
function:
Note: We need to import
Foundation
in our code to use theceil()
function. We can import it usingimport Foundation
.
ceil(number)
This function requires a number
as a parameter.
ceil()
returns the nearest, smallest integer value greater than or equal to a number sent as a parameter.
The code below shows the use of the ceil()
function in Swift:
import Swift//Header for functionimport Foundation//Integerprint ("The value of ceil(4.0) : ",ceil(4.0));//Positive double valueprint ("The value of ceil(2.56) : ",ceil(2.56));//Negative double valueprint ("The value of ceil(-2.56) : ",ceil(-2.56));
Line 3: We add the Foundation
header required for the ceil()
function.
Line 6: We calculate the ceiling of the integer value using ceil()
.
Line 9: We calculate the ceiling of the positive double value using ceil()
.
Line 12: We calculate the ceiling of the negative double value using ceil()
.