Start learning free

LESSON 22 OF 348 · CALCULATIONS WITH NUMBERS

Price After Discount in TypeScript: Working with Percentages

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

    TYPESCRIPT · CALCULATIONS WITH NUMBERS

    Price After Discount in TypeScript, explained

    Problem overview

    The starter code gives the original price of a product in dollars and a discount percentage. The task is to print the price after the discount has been applied.

    let price: number = 70;
    let discount: number = 15;
    

    The task's example uses a price of 150 and a discount of 10. A 10% discount takes 15 dollars off 150, so you pay 135. With the starter values, the answer is 59.5.

    Two ways to the same answer

    You can reach the final price in two ways, and it is useful to see that they agree.

    Subtract the discount amount

    First find how many dollars the discount is worth, then take that away. The amount is price * discount / 100, and the final price is price minus that amount. For the example, 10% of 150 is 15, and 150 - 15 is 135.

    Pay the fraction you keep

    If 10% is taken off, you still pay 90% of the price. In general you pay 100 - discount percent. So the price after the discount is the price times (100 - discount), divided by 100. This is a single expression with no intermediate value, and it is the one the lesson uses.

    Step by step

    1. Find the percentage you pay. 100 - discount is 85 for the starter values.
    2. Multiply by the price. Because the subtraction must happen first, it goes in parentheses: price * (100 - discount). Without the parentheses, price * 100 - discount would multiply first and then subtract 15, which is a different calculation.
    3. Divide by 100 to turn the percentage into a part of the price. 70 times 85 is 5950, and divided by 100 that is 59.5.
    4. Print it with console.log.
    let price: number = 70;
    let discount: number = 15;
    
    console.log((price * (100 - discount)) / 100);
    

    It prints:

    59.5
    

    Notes on the expression

    • Parentheses do real work. The inner pair around 100 - discount changes the result. The outer pair, around the multiplication, only helps readability, because * and / already run from left to right.
    • Division keeps decimals. A number in TypeScript is a floating-point value, so 5950 / 100 is 59.5 and not 59. There is no integer division to worry about.
    • Multiply before dividing. Doing the multiplication first keeps the intermediate values as whole numbers when the inputs are whole, so rounding errors are less likely. The final division is the only step that can produce a fraction.
    • The same idea works with a fraction. price * (1 - discount / 100) is another way to write it. For the starter values it also gives 59.5, but it can leave tiny rounding errors for other inputs: with a price of 1 and a discount of 7, it prints 0.9299999999999999 while the lesson's form prints 0.93. That is why the lesson's form is safer.

    Common mistakes

    • Printing the discount amount. That is the previous exercise's answer, 10.5. Here the task wants what remains to pay, 59.5.
    • Missing parentheses. price * 100 - discount / 100 computes 7000 - 0.15, which is 6999.85. The subtraction must happen inside parentheses so that it is done first.
    • Forgetting the division. A result like 5950 is 100 times too large.
    • Using the percentage as a fraction. price * discount multiplies by 15 instead of 0.15. The percentage must be divided by 100 somewhere.
    • Hard-coding numbers. Keep the names price and discount in the expression so that the answer follows the variables.

    Testing

    • price = 150, discount = 10 gives 135.
    • price = 200, discount = 50 gives 100.
    • price = 80, discount = 0 gives 80, because no discount means the full price.
    • price = 70, discount = 15 gives 59.5.

    Key takeaways

    • After a discount of d percent you pay (100 - d) percent of the price.
    • Parentheses make the subtraction happen before the multiplication.
    • The result may have decimals, and that is expected.
    Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free