Blog / June 21, 2022 / 3 mins read / By Suneet Agrawal

Round vs Floor vs Ceil : Swift

Rounding up to the nearest Integer value functionality is something required a lot of times. Swift has a few inbuilt functions which can do the rounding up for us but they are a bit confusing. To decide when to use what, we need to understand which rounding function rounds up in which direction and which data types it can round up.

Let’s understand them in detail before comparing.

Before reading this blog, keep in mind that -3 is bigger than -4 and -3.5 is bigger than -3.51

round()

  • round rounds up to the nearest Integer which can be above or below or even equal to the actual value.
  • In the case of a positive number, anything which is equal to or below x.49 will be converted to a lower number ie x.00 whereas anything which is equal to or more than x.5 will be converted to a higher number ie x+1.00
  • In the case of a negative number, anything which is equal to or below -x.5 will be converted to a lower number ie -x-1.00 whereas anything which is equal to or more than -x.49 will be converted to a higher number ie -x.00
print(round(3.00))
//this will print: 3

print(round(3.49))
//this will print: 3

print(round(3.5))
//this will print: 4

print(round(-3.00))
//this will print: -3

print(round(-3.49))
//this will print: -3

print(round(-3.5))
//this will print: -4

floor()

  • floor rounds up to the nearest Integer which can be equal to or below the actual value.
  • In the case of a positive number, anything between x.01 to x.99 will be converted to a lower number ie x.00 whereas x.00 will remain the same x.00
  • In the case of a negative number, anything between -x.01 to -x.99 will be converted to a lower number ie -x-1.00 whereas -x.00 will remain the same -x.00
print(floor(3.00))
//this will print: 3

print(floor(3.01))
//this will print: 3

print(floor(3.99))
//this will print: 3
    
print(floor(-3.00))
//this will print: -3

print(floor(-3.01))
//this will print: -4

print(floor(-3.99))
//this will print: -4

ceil()

  • ceil rounds up to the nearest Integer which can be equal to or above the actual value.
  • In the case of a positive number, anything between x.01 to x.99 will be converted to an upper number ie x+1.00 whereas x.00 will remain the same x.00
  • In the case of a negative number, anything between -x.01 to -x.99 will be converted to an upper number ie -x.00 whereas -x.00 will remain the same -x.00
print(ceil(3.00))
//this will print: 3

print(ceil(3.01))
//this will print: 4

print(ceil(3.99))
//this will print: 4

print(ceil(-3.00))
//this will print: -3

print(ceil(-3.01))
//this will print: -3

print(ceil(-3.99))
//this will print: -3

Common between round, floor and ceil

  • All three work on both Double as well as on Float.
  • Although they round to the nearest Integer, the output still remains Double in the case of Double and Float in the case of Float.

Difference between round, floor and ceil

round rounds up to the nearest Integer which can be above, below or equal to the actual value.

  • x.0 to x.49 -> x.0
  • x.5 to x.99 -> x+1.0
  • -x to -x.49 -> x.0
  • -x.5 to -x.99 -> -x-1.0

floor rounds up to the nearest Integer which can be equal to or below the actual value.

  • x.0 to x.99 -> x.0
  • -x.01 to -x.99 -> -x-1.0

ceil rounds up to the nearest Integer which can be equal to or above the actual value.

  • x.01 to x.99 -> x+1.0
  • -x.01 to -x.99 -> -x.0

The third digit after decimal doesn’t make any difference.

Comments