Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Can't get the decimal format to format correctly...

  1. #1
    Valerie1067 is offline Member
    Join Date
    Mar 2012
    Location
    Palestine Texas
    Posts
    18
    Rep Power
    0

    Default Can't get the decimal format to format correctly...

    Hi everyone!

    I am a new student and had to write a program for school where I had to show how many rolls or wallpaper a client needed for an room area, and how much it would cost (customer picks the price). and I can't get my decimal format to work properly. What i have declared as floats must remain as floats..

    Can anyone help with that please?

    here is the code that I have...

    Java Code:
    import java.util.Scanner;
    import java.text.DecimalFormat;
    
    public class WallpaperRolls
    	{
    	public static void main(String[] args)
    		{
    	//Create Scanner object
    	Scanner input = new Scanner(System.in);
    
    	//Create variables
    	float lengthOfRoom;
    	float heightOfRoom;
    	float widthOfRoom;
    	double roomArea, totalArea;
    	double roomExtra, numberOfRolls;
    	double totalCost, pricePerRoll;
    
    	DecimalFormat format = new DecimalFormat("###.00");
    
    	//Get room dimensions
    	System.out.print("What is the length of the room (in ft)?: ");
    	lengthOfRoom = input.nextFloat();
    
    	System.out.print("What is the height of the room (in ft)?: ");
    	heightOfRoom = input.nextFloat();
    	System.out.print("What is the width of room (in ft)?: ");
    	widthOfRoom = input.nextFloat();
    
    	// Get price per roll
    	System.out.print("What is the price of the wallpaper?:$");
    	pricePerRoll = input.nextDouble();
    
    	//Calculate area
    	roomArea = ((2 * lengthOfRoom * heightOfRoom) + (2 * lengthOfRoom * widthOfRoom));
    
    	//Calculate mistakes
    	roomExtra = ( roomArea*1.10);
    
    	//Calculate number of rolls
    	numberOfRolls = (roomExtra/35.0);
    		if(numberOfRolls >0)
    			{
    				numberOfRolls = numberOfRolls +1;
    			}
    
    				numberOfRolls = (roomExtra / 35);
    				System.out.println("The total number of rolls needed: " + numberOfRolls);
    
    	//Calculate total area
    	totalArea = roomArea;
    	System.out.println("The total area of room is " + totalArea);
    
    	//Calculate total cost
    	totalCost = (numberOfRolls * pricePerRoll);
    	System.out.println("The total cost of wallpaper:$" + totalCost);
    
    	}	
    }
    The output i get is for example:
    The total number of rolls needed: 18.55542933872768
    The total area of room is 590.4000244140625
    The total cost of wallpaper:$1243.2137656947546
    Last edited by Fubarable; 03-25-2012 at 02:42 AM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Can't get the decimal format to format correctly...

    You have created a DecimalFormat object, but I don't see you using it anywhere (please correct me if I'm wrong). You need to use the format(...) method of this object for it to work.

    Code tags added to your post.

  3. #3
    Valerie1067 is offline Member
    Join Date
    Mar 2012
    Location
    Palestine Texas
    Posts
    18
    Rep Power
    0

    Default Re: Can't get the decimal format to format correctly...

    Fubarable, thank you so much for replying! but I am really new to programming and not sure what you mean by "you need to use the format(...) method, Can you explain? sorry, and thank you!!!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Can't get the decimal format to format correctly...

    Again, you've created a DecimalFormat object, but that's only part of what you have to do. For it to help you out, you must call the format(...) method of this object. You feed in the floating point number into the method's parameter, and it will return a String representation of the formatted number. For example

    Java Code:
          DecimalFormat myFormatter = new DecimalFormat("###.00");
          
          double floatingPointNumber = 3.141592653589793;
          String formattedNumberString = myFormatter.format(floatingPointNumber);
          System.out.println("Un-formatted number String is: " + floatingPointNumber);
          System.out.println("Formatted number String is: " + formattedNumberString);

  5. #5
    Valerie1067 is offline Member
    Join Date
    Mar 2012
    Location
    Palestine Texas
    Posts
    18
    Rep Power
    0

    Default Re: Can't get the decimal format to format correctly...

    OK! Thank you so much for your help! I appreciate it!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Can't get the decimal format to format correctly...

    You're welcome. Next time you get stuck on the use of a class, go to the API for that class and study it as often you'll find your answer quickly there.
    Valerie1067 likes this.

Similar Threads

  1. Replies: 0
    Last Post: 02-09-2012, 09:25 AM
  2. Replies: 7
    Last Post: 09-14-2011, 03:06 PM
  3. how to convert one format to another format
    By mahipal_reddy621 in forum New To Java
    Replies: 1
    Last Post: 12-02-2008, 10:21 AM
  4. Format
    By 2ndis1stplaceloser in forum New To Java
    Replies: 2
    Last Post: 10-21-2008, 06:38 AM

Posting Permissions

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