Problem overview
The program has two f64 variables: price, the original price of a product in dollars, and discount, a percentage. It must print the price a customer pays after the discount is applied.
With price = 150 and discount = 10, the discount is 10% of 150, which is 15 dollars, and 150 - 15 gives 135. In the editor the values are 70.0 and 15.0, and the price after the discount is 59.5.
Two ways to the same answer
Subtract the discount. Compute the discount amount, price * discount / 100.0, and take it off the price: price - price * discount / 100.0. This follows the example's wording step by step.
Multiply by what you keep. If the discount is 15%, the customer still pays the other 85%. That share is 100.0 - discount percent, so the final price is the price times that percentage, divided by 100:
price after = price * (100 - discount) / 100
150 * (100 - 10) / 100 = 150 * 90 / 100 = 135
The second form needs one subtraction, one multiplication and one division, so it is shorter and has less room for a slip. The lesson uses it.
Both approaches always agree, because price - price * d / 100 is the same as price * (1 - d / 100), which is price * (100 - d) / 100. Use whichever reads more naturally to you. Floating-point rounding can make the last digits differ slightly between the two forms for awkward values, which is one more reason to round money when you display it.
Step by step
- Find the share that is paid:
100.0 - discount. For 15 that is 85. - Put it in parentheses. Without them, multiplication runs first and
price * 100.0 - discountmeans something entirely different. The parentheses force the subtraction to happen first. - Multiply by the price: 70 * 85 = 5950.
- Divide by
100.0: 5950 / 100 = 59.5. - Print the result with
println!and a{}placeholder.
The solution in Rust
let price: f64 = 70.0;
let discount: f64 = 15.0;
println!("{}", price * (100.0 - discount) / 100.0);
This prints 59.5.
Why the constants are written 100.0
Rust doesn't mix integers and floats in one expression. discount is an f64, so 100.0 - discount is fine, but 100 - discount fails to compile with error[E0277]: cannot subtract `f64` from `{integer}`. Dividing by an integer literal fails the same way. Every constant in the expression needs a decimal point.
How floats print
- A whole-number result prints without a decimal point. For
150.0and10.0the output is135, not135.0. - Floating-point numbers are binary approximations, so some amounts print with extra digits. For a price of
19.99and a discount of15.0, the expression prints16.9915. Use{:.2}in the format string to round the display to cents; it prints16.99.
Why not use integers
With i32 variables the same formula would run integer division, which drops the fraction. For 70 and 15, 70 * (100 - 15) / 100 is 59 instead of 59.5. The alternative price * ((100 - discount) / 100) is worse: with integers the division inside the parentheses is 0, so the price becomes 0.
Checking the result by hand
Before running anything, estimate the answer. A 15% discount on 70 dollars is a bit more than 10 dollars, since 10% of 70 is 7 and 5% is 3.5, which together make 10.5. The price after the discount should therefore be a little under 60, and 59.5 fits. Estimating first catches mistakes such as a missing parenthesis, which would produce a wildly different number.
Edge cases and common mistakes
- Printing the discount amount instead.
price * discount / 100.0answers the previous exercise, not this one. - Forgetting the parentheses.
price * 100.0 - discount / 100.0computes 7000 - 0.15 and prints 6999.85. - No discount. With
discount = 0.0the output equals the price. With100.0the output is 0. - An integer constant next to an
f64, which is the compile error shown above.
Testing
price = 150.0,discount = 10.0prints135.price = 70.0,discount = 15.0prints59.5.price = 80.0,discount = 50.0prints40.
