Problem overview
Two variables hold a product's original price in dollars, 70.0, and a discount, 15.0. The discount is a percentage, not a dollar amount. The task is to print how many dollars the discount takes off. For a price of 150 and a discount of 10, the answer is 15, because 10% of 150 is 15.
Approach
A percentage means "out of 100", so 15% is the fraction 15 / 100. Taking that fraction of the price gives the discount in dollars: price * discount / 100. With the example, 150 times 10 is 1500, and divided by 100 that is 15.
There is a second way to write it, price * (discount / 100), which also works with Double values. Multiplying first and dividing last is the form the lesson uses, and it is the safer habit: with integers, the multiplication happens before any digits could be dropped.
Step by step
- Multiply the price by the percentage.
price * discountis 70.0 times 15.0, which is 1050.0. - Divide by 100. This turns "percent" into dollars. The multiplication and division have the same precedence, so Kotlin runs them from left to right, which is the order we want.
- Print the result with
println. The expression goes straight into the call.
The solution in Kotlin
val price: Double = 70.0
val discount: Double = 15.0
println(price * discount / 100)
The program prints 10.5. Details that matter:
- The variables are
Double. ADoubledivided by the integer literal100is aDouble, so the fraction is kept. The output is10.5and not10. - Whole results still print a decimal point. With a price of 150.0 and a discount of 10.0, the output is
15.0, not15. - Integers would lose the fraction. If both were
Intvalues, 70 times 15 divided by 100 would print10, since integer division drops the .5. Use100.0as the divisor in that case:price * discount / 100.0prints10.5. - Mind the grouping with integers.
price * (discount / 100)withIntvalues computes15 / 100first, which is 0, and prints0. - Decimal values are approximate. A
Doublestores numbers in binary, so some results show a tiny error:33.3 * 3.0 / 100prints0.9989999999999999. The values in this lesson print cleanly.
Related calculations
The same fraction logic gives other useful numbers. Once you have the discount amount in a variable, the final price is one subtraction away:
val price = 70.0
val discount = 15.0
val amount = price * discount / 100
println("Discount: $amount") // Discount: 10.5
println(price - amount) // 59.5
This works because every value is a Double. When the inputs are whole-number Int variables, the precision question comes back. Compare these three lines for a price of 70 and a discount of 15:
println(70 * 15 / 100) // 10
println(70 * 15 / 100.0) // 10.5
println(70 * (15 / 100)) // 0
The first loses the .5 because both sides of the division are integers. The second keeps it because the divisor is a Double. The third divides 15 by 100 first, gets 0 and multiplies by 0. This is why the order of operations and the types of the operands both matter when you work with percentages.
Edge cases and common mistakes
- Forgetting to divide by 100.
price * discountprints 1050.0, the price multiplied by 15, which is not a dollar amount. - Dividing by 10. A percent is out of 100.
- Printing the final price. This lesson asks for the amount taken off, not the price after the discount.
- Zero discount. A discount of 0.0 gives 0.0, which is correct.
Testing
price = 150.0,discount = 10.0prints15.0.price = 80.0,discount = 15.0prints12.0.price = 70.0,discount = 15.0prints10.5.
Key takeaways
- A percentage of a value is
value * percent / 100. - Division keeps its fraction only when at least one operand is a
Double. - Multiplying before dividing avoids losing digits early when the values are integers.
- Check the unit of what is asked: this lesson wants the discount in dollars, not the new price.