Blog / September 28, 2024 / 4 mins read / By Mahi Garg

Round vs Floor vs Ceil : Swift

In Swift, working with floating-point numbers often requires you to round values to their nearest whole number or perform specific types of rounding. The three primary functions that help with this are:

  • round(): Rounds a number to the nearest integer.
  • floor(): Rounds a number down to the largest integer less than or equal to the number.
  • ceil(): Rounds a number up to the smallest integer greater than or equal to the number.

In this blog post, we’ll break down the differences between these functions, how and when to use each one, and show practical examples for real-world applications.

The round() Function

The round() function in Swift rounds a floating-point number to the nearest integer. If the decimal part of the number is 0.5 or greater, round() will round up; otherwise, it rounds down.

Example:
let value1 = 5.4
let roundedValue1 = value1.rounded()
print(roundedValue1)  // Output: 5.0

let value2 = 5.6
let roundedValue2 = value2.rounded()
print(roundedValue2)  // Output: 6.0
Key points:
  • Rounding down: Numbers less than 0.5 will round down to the nearest whole number.
  • Rounding up: Numbers greater than or equal to 0.5 will round up to the nearest whole number.
Example with negative numbers:
let negativeValue1 = -5.4
print(negativeValue1.rounded())  // Output: -5.0

let negativeValue2 = -5.6
print(negativeValue2.rounded())  // Output: -6.0

With negative numbers, round() behaves similarly: if the decimal part is less than -0.5, it rounds away from zero (down in magnitude); otherwise, it rounds toward zero.

The floor() Function

The floor() function always rounds a floating-point number down to the largest integer less than or equal to the number. This behavior remains consistent regardless of the decimal part of the number.

Example:
let value1 = 5.4
let flooredValue1 = floor(value1)
print(flooredValue1)  // Output: 5.0

let value2 = 5.6
let flooredValue2 = floor(value2)
print(flooredValue2)  // Output: 5.0
Key points:
  • Always rounds down: Even if the decimal part is above 0.5, it rounds down to the nearest integer.
  • Negative numbers: For negative numbers, it rounds further away from zero.
Example with negative numbers:
let negativeValue1 = -5.4
print(floor(negativeValue1))  // Output: -6.0

let negativeValue2 = -5.6
print(floor(negativeValue2))  // Output: -6.0

For negative numbers, floor() moves the value away from zero, making the number more negative.

The ceil() Function

The ceil() function always rounds a floating-point number up to the smallest integer greater than or equal to the number. Just like floor(), ceil() works irrespective of the decimal part.

Example:
let value1 = 5.4
let ceiledValue1 = ceil(value1)
print(ceiledValue1)  // Output: 6.0

let value2 = 5.6
let ceiledValue2 = ceil(value2)
print(ceiledValue2)  // Output: 6.0
Key points:
  • Always rounds up: No matter the decimal part, it rounds up to the nearest whole number.
  • Negative numbers: For negative numbers, it rounds closer to zero.
Example with negative numbers:
let negativeValue1 = -5.4
print(ceil(negativeValue1))  // Output: -5.0

let negativeValue2 = -5.6
print(ceil(negativeValue2))  // Output: -5.0

For negative numbers, ceil() rounds the value toward zero, making it less negative.

When to Use round(), floor(), and ceil()

Understanding when to use each function depends on your specific use case.

  • round(): Use round() when you want to round a value to the nearest whole number, following the standard rounding rules (half up).

Example use case: Rounding prices, weights, or any measurement to the nearest whole number.

let price = 99.99
let roundedPrice = price.rounded()  // 100.0
  • floor(): Use floor() when you need to round a value down to the nearest integer, regardless of the decimal part.

Example use case: You might use floor() in a game or physics engine to move a character to the nearest whole coordinate below the current position.

let playerPosition = 3.8
let flooredPosition = floor(playerPosition)  // 3.0
  • ceil(): Use ceil() when you need to round a value up to the nearest integer, irrespective of the decimal part.

Example use case: Allocating memory blocks or tasks where you need to ensure that a certain amount of resources is always sufficient and not less than what’s required.

let requiredBlocks = 4.2
let allocatedBlocks = ceil(requiredBlocks)  // 5.0

Round vs Floor vs Ceil

Function Behavior (Positive Numbers) Behavior (Negative Numbers)
round() Rounds to the nearest integer (0.5 and above rounds up) Same rule, rounds away from zero if >= -0.5, else toward zero
floor() Always rounds down to the nearest whole number Always rounds down (away from zero)
ceil() Always rounds up to the nearest whole number Always rounds up (toward zero)

Conclusion

In Swift, round(), floor(), and ceil() provide powerful tools for managing floating-point numbers in different contexts. Understanding when and how to use each function can significantly impact the precision and correctness of your calculations. Whether you’re working with prices, positions in a game, or even resource allocation, these rounding functions help ensure your numbers are handled properly.

By using these functions effectively, you can write more robust and error-free code that deals with floating-point rounding issues gracefully!

Comments