Learn Courses My Dashboard

14 Day Beginner - Lesson 7 Challenge

So, all is going well so far, I’m just curious about why I don’t get an exact answer for the printTotalWithTax challenge. Here’s my function and calling code:

func printTotalWithTax(subtotal:Double) {
    print(subtotal * 1.13)
}

printTotalWithTax(subtotal: 100)

The output I get is:

112.99999999999999

Why isn’t it just 113? (possibly with loads of zeros, since it’s a Double)

@BinaryForever

Hi Vince,

Welcome to the community.

It’s just the way that Swift deals with Doubles. There is no loss of accuracy. If you use the round() method in your function like this:

func printTotalWithTax(subtotal:Double) {
    print(round(subtotal * 1.13))
}

you will get the output 113.0