New to swift. How do I get the following code to work?

I am trying to write a program to give total and average on commissions. Between certain amounts the commission is more but I can't seem to figure out how to make it apply right. It isn't keeping between the ranges I set, example if I put in value 2500.00 it applys the 10% and stops instead of going to the 20% and stopping. Not sure how to make it stay in the range to apply the right percentage.



/

repeat {

print("Enter your sales amount: ", terminator: "")

let n_str = input() /

let t = NSString(string: n_str) /

sales = t.doubleValue

if sales <= 999.99 {

sales = sales * 0.05

} else if sales >= 1000.00 - 1999.99 {

sales = sales * 0.10

} else if sales >= 2000.00 - 2999.99 {

sales = sales * 0.20

} else if sales >= 3000.00 - 3999.99 {

sales = sales * 0.30

} else if sales >= 4000.00 - 4999.99 {

sales = sales * 0.40

} else if sales >= 5000.00 {

sales = sales * 0.50

}

total += sales /

/

count += 1

}while count < numSales

Replies

Your tests are not correctly written

When you write :


if sales >= 1000.00 - 1999.99 {

it means sales > -999.99


You should write :


if sales >= 1000.00 && sales <= 1999.99 {

You seem to treat "sales >= 1000.00 - 1999.99" as a way to check if the sales are between 1000.00 and 1999.99. However, this first calculates 1000.00 - 1999.99, which is -999.99, and then compares -999.99 to the amount you have entered. This will always be true, so all amounts above 999.99 will have 10% commission.


If you change the first "else if" condition to "sales <= 1999.99" and the next to "sales <= 2999.99", and so on, it should work as you expect. The way you have written the code, it will stop after the first matching condition has been reached, so you don't have to check the previous conditions again.


The last "else if sales >= 5000.00" can be changed to just "else", since you are interested in everything that hasn't been matched yet.

Well, you're doing a subtraction instead of testing a range. An expression like "1000.00 - 1999.99" evaluates to -999.99 (a negative number). Since you've already determined that 'sales' with the value 2500.00 is not less than 999.99 (the first 'if'), your next test is for sales greater than or equal to -999.99, which always succeeds, so you always apply the 10%.


The most straightward way to write the test you want is like this:


if sales < 1000.00 {
        sales = sales * 0.05
} else if sales >= 1000.00 && sales < 2000.00 {


Note that I also took the opportunity to avoid constants ending in .99, since that isn't exactly representible as a Double value, so you might otherwise trip yourself up if you're not careful.

The most straightward way to write the test you want is like this:

Why not use a

switch
statement?
let salesList: [Double] = [0, 500.00, 999.99, 1000.00, 1500.00, 1999.99, 2000.00, 2500.00]
for sales in salesList {
    let tax: Double
    switch sales {
        case 0..<1000.00:
            tax = 0.05
        case 1000.00..<2000.00:
            tax = 0.10
        default:
            tax = 0.20
    }
    print("\(sales) -> \(tax)")
}

which prints:

0.0 -> 0.05
500.0 -> 0.05
999.99 -> 0.05
1000.0 -> 0.1
1500.0 -> 0.1
1999.99 -> 0.1
2000.0 -> 0.2
2500.0 -> 0.2

Note that I also took the opportunity to avoid constants ending in .99, since that isn't exactly representible as a Double value, so you might otherwise trip yourself up if you're not careful.

Right. NSDecimalNumber is your friend when dealing with financial calculations. Or, in a simple case like this, work in the ‘cent’ domain. For example:

let salesList: [Int] = [0, 500_00, 999_99, 1000_00, 1500_00, 1999_99, 2000_00, 2500_00]
for sales in salesList {
    let tax: Int
    switch sales {
        case 0..<1000_00:
            tax = 5
        case 1000_00..<2000_00:
            tax = 10
        default:
            tax = 20
    }
    print("\(Double(sales) / 100.0) -> \(tax)")
}

which prints:

0.0 -> 5
500.0 -> 5
999.99 -> 5
1000.0 -> 10
1500.0 -> 10
1999.99 -> 10
2000.0 -> 20
2500.0 -> 20

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"