Start learning free

LESSON 21 OF 415 · CALCULATIONS WITH NUMBERS

Price After Discount in Swift: Percentages with Double

Lesson Read the full explanation

We created two variables to store the original price of a product in dollars and the discount percentage applied to that price.

Use these two variables and arithmetic operations to compute and print the price after the discount.


Example:

For example, if price was 150 and discount was 10, the answer would be 135.

Why? Because the discount is 10%. 10% of 150 dollars is 15 dollars. And 150 - 15 dollars is 135 dollars.

STEP 1 OF 1
    Ctrl + Enter to check

    SWIFT · CALCULATIONS WITH NUMBERS

    Price After Discount in Swift, explained

    Problem overview

    The starter code gives the original price of a product and a discount percentage. The program must print the price the customer pays after the discount is taken off.

    With a price of 150 and a discount of 10, the discount is 10% of 150, which is 15, so the customer pays 150 - 15 = 135. The starter values are 70 and 15, so the answer to compute is 85% of 70.

    Approach

    There are two ways to see the same result:

    • Work out the discount amount, price * discount / 100, and subtract it from price.
    • Work out what is left to pay directly. A 15% discount means the customer pays 85% of the price, and 85 is 100 - discount. So the price after the discount is price * (100 - discount) / 100.

    The lesson uses the second way, which needs only one expression.

    Step by step

    1. Compute the share that is still paid. 100 - discount is 85 for a discount of 15. Parentheses are needed here, because without them multiplication would run before the subtraction.
    2. Multiply by price. 70 times 85 is 5950.
    3. Divide by 100 to turn the percentage back into a fraction: 5950 divided by 100 is 59.5.
    4. Print the expression. No variable is needed for it.

    The solution in Swift

    let price = 70.0
    let discount = 15.0
    
    print(price * (100 - discount) / 100)
    

    The output is:

    59.5
    

    The Swift details worth noticing:

    • The type comes from the literal. A number written with a decimal point, such as 70.0, makes Swift infer Double. Writing 70 would infer Int.
    • Literals adapt in the expression. The 100 in 100 - discount and in / 100 becomes a Double because the values next to it are Double. No conversion is written.
    • Typed values don't mix. If discount were an Int constant and price a Double, price * (100 - discount) wouldn't compile, because the parentheses would hold an Int. Convert explicitly with Double(discount).
    • Output format. A Double prints with a decimal part, so a whole result such as the task's example, 150 and 10, prints as 135.0.

    Edge cases and common mistakes

    • Missing parentheses. price * 100 - discount / 100 multiplies and divides first, and gives 7000 - 0.15 = 6999.85.
    • Subtracting the percentage directly. price - discount takes 15 dollars off, not 15 percent.
    • Using integers. With Int constants, results that need a fraction are cut off. For a price of 70 and a discount of 15, price * (100 - discount) / 100 evaluates to 5950 / 100, which is 59, not 59.5. The decimal point in the starter code prevents that.
    • Printing the discount amount. price * discount / 100 is the previous exercise's answer: 10.5. This task wants the amount that remains.
    • A 0% or 100% discount. At 0 the price stays at 70.0, and at 100 the result is 0.0. The formula covers both without a special case.

    Testing

    • price = 150.0, discount = 10.0 prints 135.0.
    • price = 70.0, discount = 15.0 prints 59.5.
    • price = 80.0, discount = 0.0 prints 80.0.

    Key takeaways

    • Paying after a discount of d percent means paying 100 - d percent of the price.
    • Parentheses make the subtraction happen before the multiplication.
    • A decimal point in a literal makes it a Double, which keeps fractions.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free