Start learning free

LESSON 19 OF 348 · CALCULATIONS WITH NUMBERS

Find the Discount Amount in Kotlin

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

    KOTLIN · CALCULATIONS WITH NUMBERS

    Find Discount in Kotlin, explained

    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

    1. Multiply the price by the percentage. price * discount is 70.0 times 15.0, which is 1050.0.
    2. 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.
    3. 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. A Double divided by the integer literal 100 is a Double, so the fraction is kept. The output is 10.5 and not 10.
    • Whole results still print a decimal point. With a price of 150.0 and a discount of 10.0, the output is 15.0, not 15.
    • Integers would lose the fraction. If both were Int values, 70 times 15 divided by 100 would print 10, since integer division drops the .5. Use 100.0 as the divisor in that case: price * discount / 100.0 prints 10.5.
    • Mind the grouping with integers. price * (discount / 100) with Int values computes 15 / 100 first, which is 0, and prints 0.
    • Decimal values are approximate. A Double stores numbers in binary, so some results show a tiny error: 33.3 * 3.0 / 100 prints 0.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 * discount prints 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.0 prints 15.0.
    • price = 80.0, discount = 15.0 prints 12.0.
    • price = 70.0, discount = 15.0 prints 10.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.
    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free