# Forcing two decimal places (IE adding 0s where there are none)

• 09-09-2012, 07:15 AM
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!)

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));                 }         }```
• 09-09-2012, 07:35 AM
pbrockway2
Re: Forcing two decimal places (IE adding 0s where there are none)
Quote:

DecimalFormat f = new DecimalFormat("############.##");
Read the DecimalFormat API docs to see what options you have for displaying digits. At the moment you are using # which means "Digit, zero shows as absent", and you don't want it to be absent.
• 09-09-2012, 07:48 AM
Re: Forcing two decimal places (IE adding 0s where there are none)
Quote:

Originally Posted by pbrockway2
Read the DecimalFormat API docs to see what options you have for displaying digits. At the moment you are using # which means "Digit, zero shows as absent", and you don't want it to be absent.

Well now I feel dumb. I had glanced over that quickly but somehow managed to miss that entire section. Sorry for wasting your time with such a simple answer and thanks for the help!
• 09-09-2012, 09:47 AM
pbrockway2
Re: Forcing two decimal places (IE adding 0s where there are none)
Glad you've got it sorted out. I hoped quoting "shows as absent" would be enough to point you to the right part of the docs.

It's a bl00dy silly wording, though. I mean, a thing shows, or it is absent.