Start learning free

LESSON 20 OF 414 · CALCULATIONS WITH NUMBERS

Find Discount in Rust: Percentages with f64

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

    RUST · CALCULATIONS WITH NUMBERS

    Find Discount in Rust, explained

    Problem overview

    The program has two variables: price, the original price of a product in dollars, and discount, a percentage. It must print how many dollars the discount is worth, which is the discount amount, not the new price.

    Take price = 150 and discount = 10. A 10% discount on 150 dollars is 15 dollars, so the answer is 15. In the editor the values are 70.0 and 15.0, and the answer is 10.5.

    How to think about it

    A percentage is a fraction of 100. Ten percent means 10 out of every 100, or 10 / 100 = 0.1 of the price. So the discount amount is the price times the percentage, divided by 100:

    amount = price * discount / 100
    150 * 10 / 100 = 1500 / 100 = 15
    

    Multiplication and division share the same level and run from left to right, so price * discount / 100.0 first multiplies and then divides. The result would be the same with the division written first as price * (discount / 100.0) for these values, but multiplying first is the common choice, because it keeps whole numbers whole for as long as possible.

    A second check makes the idea concrete: a 50% discount on 80 dollars should be half the price, and indeed 80 * 50 / 100 = 4000 / 100 = 40. A 100% discount takes off the whole price, and a 0% discount takes off nothing. Testing a formula against such easy cases before trusting it on awkward numbers is a habit worth building.

    Step by step

    1. Multiply price by discount. For 70 and 15 that is 1050.
    2. Divide by 100.0 to turn the percentage into dollars: 1050 / 100 = 10.5.
    3. Print the expression with println! and the "{}" format string.

    Why 100.0 and not 100

    Both variables are declared as f64, Rust's 64-bit floating-point type, so a price like 70.5 or a discount like 12.5 is allowed. Rust never mixes floats and integers in arithmetic, so the divisor has to be a float too. Writing / 100 fails to compile:

    error[E0277]: cannot divide `f64` by `{integer}`
    

    A literal with a decimal point, such as 100.0, has type f64, which fixes the error. You don't need a cast or a suffix.

    The solution in Rust

    let price: f64 = 70.0;
    let discount: f64 = 15.0;
    
    println!("{}", price * discount / 100.0);
    

    This prints 10.5. Details about how Rust prints floats:

    • A float with no fractional part prints without a decimal point: with price = 150.0 and discount = 10.0 the output is 15, not 15.0. The debug format {:?} would print 15.0.
    • Floats are binary approximations, so some results show extra digits. With a price of 19.99 and a discount of 15.0 the same expression prints 2.9984999999999995. To show cents, use a precision in the placeholder: println!("{:.2}", ...) prints 3.00.

    Why integers would be a trap

    If the variables were i32, the same formula with integer division would drop the fraction. With 70 and 15, 70 * 15 / 100 is 10, not 10.5. And the shortcut of dividing first, discount / 100, gives 0 for any whole discount below 100, so the whole result collapses to 0. Floats avoid both problems.

    Reading the starter code

    The two lines declare their types explicitly with : f64. That is optional here, because a literal like 70.0 already defaults to f64, but it documents the intent. Variables in Rust are immutable by default, so the program only reads price and discount and prints a new value, never modifying either.

    Edge cases and common mistakes

    • A zero discount prints 0, and a discount of 100 prints the whole price.
    • Printing the new price. This lesson asks only for the amount taken off. A related exercise subtracts it from the price.
    • Forgetting the decimal point in the divisor, which is the compile error above.
    • Treating the percentage as a fraction. price * discount alone gives 1050, which is 100 times too large.

    Testing

    • price = 150.0, discount = 10.0 prints 15.
    • price = 70.0, discount = 15.0 prints 10.5.
    • price = 80.0, discount = 0.0 prints 0.
    Keep going with RustAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free