Swift Tutorial Swift References

Swift - floor() Function



The Swift floor() function returns the next lowest integer value by rounding down the specified number, if necessary. In other words, it rounds the fraction DOWN of the given number.

Syntax

In Foundation framework, it is defined as follows:

public func floor(_ x: CGFloat) -> CGFloat
public func floor(_ x: Float) -> Float
public func floor(_ x: Float80) -> Float80
public func floor(_ __x: Double) -> Double

Parameters

x Specify a number.

Return Value

Returns the next lowest integer value by rounding DOWN the specified number, if necessary.

Example:

In the example below, floor() function is used to round the fraction DOWN of the specified number.

import Foundation

print("floor(-10.2) = \(floor(-10.2))")
print("floor(-10.8) = \(floor(-10.8))")
print("floor(5.2) = \(floor(5.2))")
print("floor(5.8) = \(floor(5.8))")

The output of the above code will be:

floor(-10.2) = -11.0
floor(-10.8) = -11.0
floor(5.2) = 5.0
floor(5.8) = 5.0

❮ Swift Math Functions