Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

swift
ceil
math
communitycreator

What is the ceil() function in Swift?

Harris Amjad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

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:

Mathematical representation of ceil() function

Note: We need to import Foundation in our code to use the ceil() function. We can import it using import Foundation.

Syntax


ceil(number)

Parameter

This function requires a number as a parameter.

Return value

ceil() returns the nearest, smallest integer value greater than or equal to a number sent as a parameter.

Example

The code below shows the use of the ceil() function in Swift:

import Swift
//Header for function
import Foundation
//Integer
print ("The value of ceil(4.0) : ",ceil(4.0));
//Positive double value
print ("The value of ceil(2.56) : ",ceil(2.56));
//Negative double value
print ("The value of ceil(-2.56) : ",ceil(-2.56));

Explanation

  • 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().

RELATED TAGS

swift
ceil
math
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring