Results 1 to 4 of 4
 09092012, 07:15 AM #1Member
 Join Date
 Sep 2012
 Posts
 8
 Rep Power
 0
Forcing two decimal places (IE adding 0s where there are none)
I've been working on an assignment and have it 99% of the way complete, but I need to format my final results (Money) into always have two decimal places, subsequently needing to add a 0. I've forced it to round always, but cannot for the life of me figure out how to make it always add a 0. Any help?
Code so far is here: (Sorry for it being a bit clustered!)
Java Code:import java.text.DecimalFormat; import java.util.Scanner; public class JavaJoe { /**A program to calculate the total cost of an order of bags of coffee beans. * @author Anonymous * @since 9/8/12 */ private static Scanner keyboard; public static void main(String[] args) { DecimalFormat f = new DecimalFormat("############.##"); keyboard = new Scanner(System.in); System.out.println("How many bags would you like to purchase?"); String bagsS = keyboard.nextLine(); int bags = Integer.parseInt(bagsS); //System.out.println(bags); double discount = 0; if (bags >= 300){ discount = (double) .3;} else if (bags >= 250){ discount = (double) .25;} else if (bags >= 200){ discount = (double) .20;} else if (bags >= 100){ discount = (double) .15;} else if (bags >= 50){ discount = (double) .1;} else if (bags >= 25){ discount = (double) .05;} //Figuring out the bag price and the discount); double bagPrice = (bags*5.50); double bagDiscount = (bagPrice * discount); //Finding the number of boxes int largeBoxes = (bags / 20); //System.out.print(largeBoxes); int moreBoxes = (bags % 20); int mediumBoxes = (moreBoxes / 10); //System.out.print(mediumBoxes); int smallBoxesP1 = (moreBoxes % 10); int smallBoxes = 0; if (smallBoxesP1 > 5){ int smallBoxesP2 = 2; smallBoxes = smallBoxesP2;} else if (smallBoxesP1 == 0){ int smallBoxesP2 = 0; smallBoxes = smallBoxesP2;} else if (smallBoxesP1 >0){ int smallBoxesP2 = 1; smallBoxes = smallBoxesP2;} double lBoxCost = (largeBoxes * 2.00); double mBoxCost = (mediumBoxes * 1.00); double sBoxCost = (smallBoxes * 0.50); double totalCostUF = (bagPrice + sBoxCost + mBoxCost + lBoxCost  bagDiscount); //Printing out all the results System.out.println("Bags ordered: " + bags + " Bags' cost: $" + f.format(bagPrice)); System.out.println("Bag discount: " + (discount*100)+"%" + " which comes out to $" + (f.format(bagDiscount))+ "."); System.out.println(""); System.out.println("Boxes needed: "); System.out.println("Large boxes used: " + largeBoxes + " costing $" + f.format(lBoxCost)+ "."); System.out.println("Medium boxes used: " + mediumBoxes + " costing $" + f.format(mBoxCost)+ "."); System.out.println("Small boxes used: " + smallBoxes + " costing $" + f.format(sBoxCost)+ "."); System.out.println(""); System.out.println("The total cost is: " + f.format(totalCostUF)); } }
 09092012, 07:35 AM #2Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,712
 Rep Power
 13
Re: Forcing two decimal places (IE adding 0s where there are none)
DecimalFormat f = new DecimalFormat("############.##");
 09092012, 07:48 AM #3Member
 Join Date
 Sep 2012
 Posts
 8
 Rep Power
 0
 09092012, 09:47 AM #4Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,712
 Rep Power
 13
Similar Threads

Rounding Decimal Places
By neverbend in forum JCreatorReplies: 4Last Post: 09292011, 11:41 PM 
3 Decimal Places
By benhawk in forum New To JavaReplies: 7Last Post: 11022010, 10:28 PM 
2 decimal places needed
By lala in forum New To JavaReplies: 5Last Post: 10272010, 08:13 PM 
numbers with two decimal places
By little_polarbear in forum New To JavaReplies: 8Last Post: 08272008, 11:04 PM 
Capping decimal places
By Rageagainst20 in forum New To JavaReplies: 1Last Post: 12202007, 10:28 PM
Bookmarks