Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Zoralink23

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

  1. #1
    Zoralink23 is offline Member
    Join Date
    Sep 2012
    Posts
    8
    Rep Power
    0

    Default 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));
    	
    	}
    	}

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Forcing two decimal places (IE adding 0s where there are none)

    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.

  3. #3
    Zoralink23 is offline Member
    Join Date
    Sep 2012
    Posts
    8
    Rep Power
    0

    Default Re: Forcing two decimal places (IE adding 0s where there are none)

    Quote Originally Posted by pbrockway2 View Post
    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!
    DarrylBurke likes this.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default 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.

Similar Threads

  1. Rounding Decimal Places
    By neverbend in forum JCreator
    Replies: 4
    Last Post: 09-30-2011, 12:41 AM
  2. 3 Decimal Places
    By benhawk in forum New To Java
    Replies: 7
    Last Post: 11-02-2010, 10:28 PM
  3. 2 decimal places needed
    By lala in forum New To Java
    Replies: 5
    Last Post: 10-27-2010, 09:13 PM
  4. numbers with two decimal places
    By little_polarbear in forum New To Java
    Replies: 8
    Last Post: 08-28-2008, 12:04 AM
  5. Capping decimal places
    By Rageagainst20 in forum New To Java
    Replies: 1
    Last Post: 12-20-2007, 10:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •