My program is supposed to calculate change:
double purchaseAmount = Double.parseDouble(s1); double amountGiven = Double.parseDouble(s2); double change = purchaseAmount - amountGiven; JOptionPane.showMessageDialog(null, change); String result = "Change consists of:"; My change is counted in bills and coins. But once I get to the coins, which are double:
double quarters = 0; if (change >= .25) { quarters = change / .25; change = change - quarters * .25; result = result + quarters + "quarters"; change = change - quarters % .25; } The result is giving me a trailing .0 for instance instead of telling me my change is 1 twenty 2 quarters. I get 1 twenty 2.0 quarters. I've tried almost everything, BigDecimal, NumberFormat DecimalFormat(##.###) I already have a String in the program and would not know how to add another.
doubleanyway? (Your line ofchange = change - quarters % .25;looks dubious to me, too...) I would actually suggest that you change the value from dollars to cents to start with, and do everything in integer arithmetic after that. It'll make life much simpler.result + quarters + "quarters"<-- thequartersvalue should be formatted to a string there according to the desired rules; using anintmight be sufficient to "solve" this issue, as such does not contain fractional information to start.BigDecimalwould be okay (if handled carefully in terms of division etc).