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
- Multiply
pricebydiscount. With the lesson's values, 70.0 * 15.0 is 1050.0. - Divide that by 100 to turn the percentage into a part of the price: 1050.0 / 100 is 10.5.
- 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 / 100is 0 for any discount under 100 when both areInt. - 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, orprice * (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. Doubleoperands keep the decimals, and the output always shows a decimal part.- With
Intoperands, multiply before dividing, or the fraction disappears too early.
