Swift Tutorial Swift References

Swift - ceil() Function



The Swift ceil() function returns the next highest integer value by rounding up the specified number, if necessary. In other words, it rounds the fraction UP of the given number.

Syntax

In Foundation framework, it is defined as follows:

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

Parameters

x Specify a number.

Return Value

Returns the next highest integer value by rounding UP the specified number, if necessary.

Example:

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

import Foundation

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

The output of the above code will be:

ceil(-10.2) = -10.0
ceil(-10.8) = -10.0
ceil(5.2) = 6.0
ceil(5.8) = 6.0

❮ Swift Math Functions