This is a kind of a newb question but I’m really new to programming and I’m only a little over a week into a programming course in grade 11. We use Java, and work with Netbeans.
What I’m working on right now is this:
During a special sale at a store, a 10% discount is taken off of
purchases over $10.00.Create an application that prompts the user for the amount of
purchases and then returns the discounted price, as well as the
discount amount itself.
My Code:
DecimalFormat x = new DecimalFormat ("$###.00");
double purchases, discount, finalPrice;
purchases = Double.parseDouble (purchaseInput.getText ());
if (purchases >= 10) {
discount = purchases * 0.10;
}
else {
discount = 0;
}
finalPrice = purchases - discount;
discountOutput.setText (discount);
finalPriceOutput.setText (finalPrice);
The problem is the last two lines. I’m trying to set the values to text fields. It says that it is finding double, but needs java.lang.String. I tried changing them to a string with double.toString () but it says the double can’t be dereferenced. I’m pretty confused right now, please help.
I’m sure the fix isn’t very complicated, but I’ve looked every content section we have learned so far twice and still cant find how to fix my problem.
You already have a
DecimalFormatto convert doubles to strings. So try this:You can also do it without using
DecimalFormat, but the result will be slightly different because the numbers are not formatted as “$###.00”:Also, as a suggestion, I think money quantities look better when you use “
$#,##0.00“. That way you have thousands separator (,) and fractions of a dollar show up like $0.51 instead of $.51, which looks weird. So do: