Blog / August 16, 2022 / 3 mins read / By Mahi Garg

Round up to n Decimal Digit: Swift

We sometimes in our calculation get results with more than 4 decimal digits or even more. But as a developer, we can’t use the same to display to users.

Imagine we have a dashboard application that displays monthly tax on an employee’s salary. And it is coming as 630.6152. This doesn’t look good at all to say the same. It will be better to show it like 630.62 or 630.61 i.e., rounding it to n Decimal Digit.

Let us take a look at how this can be done.

If we say round up to n Decimal Digits where n equals 2. Then we need to follow the below steps

  • Multiply it by 100 (10^n)
  • Use round, floor, or ceil to remove the extra digits
  • Divide by 100 (10^n)

Round to n digits using the round function

//after calculation
tax = 630.6152
//multiply with 100
var result = tax * 100                 //63061.52
//Using round to remove extra digit
roundresult = round(result)            //63062
//divide by 100
result = result/ 100             
print (result)                         //630.62
print(evenNumbers)

Here tax 630.6152 is rounded to 630.62 as the third decimal digit is 5.

Let us consider example 437.1932. Here 3 is the third decimal digit.

//after calculation
tax = 437.1932
//multiply with 100
var result = tax * 100                 //43719.32
//Using round to remove extra digit
roundresult = round(result)            //43719
//divide by 100
result = result/ 100
print (result)                         //437.19

On comparing the result of both the example, we can see the result depends on the decimal digits we have. If the third digit is between 1 to 4, then it will keep the existing previous results (437.1932 -> 437.19) but in the case of digits between 5–9, it increases the last digit (630.6152 -> 630.61).

Now let us see how our output will be impacted if we use the floor or ceil function instead of round.


Round to n digits using the floor function

Considering the same example here. Tax after calculation equals 630.6152.

//after calculation
tax = 630.6152
//multiply with 100
var result = tax * 100                 //63061.52
//Using floor to remove extra digit
floorResult = floor(result)            //63061
//divide by 100
floorResult = floorResult / 100
print (floorResult)                    //630.61

Here, we saw floor will step down the result irrespective of the digit we have at the third position, be it 1 or 9 it will always take you to the nearest smallest number.


Round to n digits using the ceil function

Here, let us alter our example to 630.6122 and see how our results vary.

//after calculation
tax = 630.6122
//multiply with 100
var result = tax * 100                 //63061.22
//Using ceil to remove extra digit
ceilResult = ceil(result)              //63062
//divide by 100
ceilResult = ceilResult / 100
print (ceilResult)                     //630.62

Here, we saw ceil will step up the result irrespective of the digit we have at the third position, be it anything between 1 to 9 it will always take you to the nearest largest number.

Comments