The floor()
function in Swift returns the next largest integer that is less than or equal to a specified number.
Figure 1 shows the mathematical representation of the floor()
function.
Note: We need to import
Foundation
in our code to use thefloor()
function. We can import it like this:import Foundation
.
floor(number)
This function requires a number as a parameter.
The floor()
function returns the next largest integer less than or equal to the number set as a parameter.
The code written below demonstrates the use of the floor()
function in Swift:
import Swift//header for functionimport Foundation//integerprint ("The value of floor(4.0) : ",floor(4.0));//positive double valueprint ("The value of floor(2.56) : ",floor(2.56));//negative double valueprint ("The value of floor(-2.56) : ",floor(-2.56));
Line 3: We add the header Foundation
required for the floor()
function.
Line 6: We calculate the floor of the integer values using the floor()
function.
Line 9: We calculate the floor of the positive double value using the floor()
function.
Line 12: We calculate the floor of the negative double value using the floor()
function.