Start learning free

LESSON 19 OF 414 · CALCULATIONS WITH NUMBERS

Find Discount in Scala: 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 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

    SCALA · CALCULATIONS WITH NUMBERS

    Find Discount in Scala, explained

    The task

    The starter code gives two values: price, the original price of a product in dollars, and discount, a percentage. You have to print the discount amount in dollars, that is, how much of the price is taken off. A discount of 10 on a price of 150 is 10% of 150, which is 15 dollars.

    Thinking in percentages

    A percentage is a fraction out of 100. So 10% means 10 / 100, and 10% of 150 means 150 * 10 / 100. The general formula is:

    discount amount = price * discount / 100
    

    Printing price or discount alone would be wrong. The first is the full price and the second is only the percentage. The answer is a part of the price, so the two values have to be combined with arithmetic.

    Step by step

    1. Multiply price by discount. With the lesson's values, 70.0 * 15.0 is 1050.0.
    2. Divide that by 100 to turn the percentage into a part of the price: 1050.0 / 100 is 10.5.
    3. Print the result with println.

    The multiplication goes first and the division second because both operators have the same rank and Scala evaluates them from left to right. Writing the expression in the other order, price * (discount / 100), gives the same value here, but the order shown is the safer habit for the reasons in the next section.

    The solution in Scala

    val price: Double = 70.0
    val discount: Double = 15.0
    println(price * discount / 100)
    

    The output is:

    10.5
    

    Why Double matters

    Both values are declared as Double, a 64-bit decimal number. When one operand of an operator is a Double, the result is a Double, so / 100 keeps its decimals. That is why the answer 10.5 appears in full. A Double always prints with a decimal part, so a whole-number result shows up as 15.0, not 15.

    With Int values the same task needs more care, because Int / Int drops the fraction. Take an Int price of 150 and discount of 10:

    val p = 150
    val d = 10
    println(p * d / 100)
    println(d / 100 * p)
    
    15
    0
    

    The first line multiplies before dividing, so 1500 / 100 gives 15. The second divides first: 10 / 100 is 0 for integers, and 0 * 150 is still 0. The order of operands changed the answer. With Int values, always multiply first and divide last. With a price of 70 and a discount of 15 as Int values, price * discount / 100 would print 10, because the .5 is dropped.

    Edge cases and common mistakes

    • Dividing by 100 too early with integers. As shown above, discount / 100 is 0 for any discount under 100 when both are Int.
    • Printing the price after the discount. This exercise asks for the amount taken off. The reduced price is a separate calculation: price - price * discount / 100, or price * (100 - discount) / 100.
    • A discount of 0 or 100. A discount of 0 gives 0.0, and a discount of 100 gives the whole price. Both work with the same formula.
    • Fixed numbers in the code. Use the variable names so the answer follows the values.

    Key takeaways

    • The discount amount is price * discount / 100.
    • Double operands keep the decimals, and the output always shows a decimal part.
    • With Int operands, multiply before dividing, or the fraction disappears too early.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free