Start learning free

LESSON 19 OF 415 · CALCULATIONS WITH NUMBERS

Find the Discount Amount in Swift with Double Arithmetic

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 discount amount in dollars.


Example:

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

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

STEP 1 OF 1
    Ctrl + Enter to check

    SWIFT · CALCULATIONS WITH NUMBERS

    Find Discount in Swift, explained

    Problem overview

    Two constants are given: price, the original price of a product in dollars, and discount, a percentage. The program must print the discount amount in dollars, not the price after the discount.

    With price equal to 150 and discount equal to 10, the discount is 10% of 150, which is 15 dollars. In the starter code the values are 70 and 15, so the program has to work out 15% of 70.

    Approach

    A percentage is a number out of 100. Taking discount percent of price means multiplying the two and dividing by 100: price * discount / 100. Multiplication and division share a precedence level and run from left to right, so the product is computed first and then divided.

    The starter code declares both constants with an explicit Double type. That choice is deliberate: prices and percentages often aren't whole numbers, and 15% of 70 is 10.5.

    Step by step

    1. Keep the two Double constants. The type annotation : Double makes the number 70 a floating-point value.
    2. Multiply price by discount. This gives the percentage scaled by 100: 70 times 15 is 1050.
    3. Divide by 100. The literal 100 takes the type of the other operand, so Swift treats it as a Double and keeps the decimal part. 1050 divided by 100 is 10.5.
    4. Pass the expression to print. No variable is needed for the result.

    The solution in Swift

    let price: Double = 70
    let discount: Double = 15
    
    print(price * discount / 100)
    

    The output is:

    10.5
    

    Swift details that matter here:

    • A literal adapts to its context. The 100 in / 100 is not an Int because it appears next to a Double. Swift infers its type from the other operand. That is why no conversion is needed.
    • No implicit conversion between variables. Literals adapt, but typed values don't. If discount were an Int constant and price a Double, price * discount would fail to compile with binary operator '*' cannot be applied to operands of type 'Double' and 'Int'. Write price * Double(discount) in that case.
    • Double output. A Double always prints with a decimal point, so the example from the task, 150 and 10, prints 15.0 rather than 15.

    Edge cases and common mistakes

    • Integer division. If both constants were Int, the division by 100 would drop the fraction. With price = 7 and discount = 10 as integers, price * discount / 100 gives 0, although the real discount is 0.7. Using Double avoids the loss.
    • Dividing first. price * (discount / 100) is mathematically the same, and with Double values it works. With integers, discount / 100 would already be 0 for any discount under 100.
    • Printing the price after the discount. This task asks for the discount amount only. Subtracting it from price is the next exercise.
    • Forgetting the division by 100. price * discount prints 1050.0, which is a percentage-times-dollars value that isn't a dollar amount.
    • Writing % for percent. In Swift, % is the remainder operator and doesn't work on Double values at all, so it can't stand in for a percentage sign.

    Testing

    • price = 150, discount = 10 prints 15.0.
    • price = 70, discount = 15 prints 10.5.
    • price = 100, discount = 0 prints 0.0.

    Key takeaways

    • A percentage of a value is value * percent / 100.
    • Use Double when the result may have a fraction.
    • Literals adapt to the type of the other operand; typed constants don't convert on their own.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free