Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: how to format double value?

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    84
    My Mood
    Daring
    Thanks
    17
    Thanked 1 Time in 1 Post

    Exclamation how to format double value?

    here is waht I'm tring to do:
    A household is preparing its next month's budget. For this, they want to estimate their monthly grocery spend. They expect their next month's grocery spend to be 10% higher than the average spend over the last several months. Given their grocery spend for the last several months, the expected spend should be rounded off to 2 decimal places. For e.g. if the input is {1004.56,1200,989.50,1100,1050.36,1000.88,1104.56, 990,1089.50,1070,1310.36,900.88}, the expected output is 1174.31
    and coding done by me
    import java.text.DecimalFormat;
    public class GroceryPlan {
    	static double[] test1 = {1004.56,1200,989.50,1100,1050.36,1000.88,1104.56,990,1089.50,1070,1310.36,900.88};
     
    	static double[] testcase = test1;
     
    	public static void main(String args[]){
    		GroceryPlan inst = new GroceryPlan();
    		double v = inst.getEstimate(testcase);
    		System.out.println(v);
    	}
     
    	// Write your code in the function below
    	public double getEstimate(double[] spends){
    	         double total=0,inc=0;
    	         DecimalFormat Form = new DecimalFormat("#.##");
    	         double count= spends.length;
    	         for(double element:spends){
    		        total+=element;
    		       }
    	         System.out.printf("total amount : %.2f\n",total);
    	         System.out.println("Months :"+count);
    	         total/=count;
    	         inc=total*0.1;
    	         total+=inc;
    	         return Double.valueOf(Form.format(total));
    	}

    output is looking fine but look here when code is check for quality
    execution.JPG

    Please guide me how to format double values? please look at the screen shot
    Last edited by arvindbis; March 10th, 2012 at 12:02 PM. Reason: formatted code


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    979
    Thanks
    7
    Thanked 206 Times in 182 Posts

    Default Re: how to format double value?

    output is looking fine but look here when code is check for quality
    First to explain what is going on...

    "Formatting" is the process of going from a numeric quantity to a string. In themselves double values don't have decimal places (or a base for that matter): they just have magnitude. Once they are formatted - ie a string is obtained - then we can talk about decimal places. In your code the format() method does this. It goes from a double to a String.

    "Parsing" is the opposite of formatting. It starts with a string and obtains a double. It takes some getting used to, but "3.14159" and 3.14159 are just different things. In particular the former has decimal places, the latter (considered as a double) does not. Parsing has to make a guess (or be told) about the base, but the information about the number of decimal places is lost. In your code the valueOf() method does the parsing.

    Your code formats the double total to obtain a string with two decimal places. But then it parses that string to obtain a double from which all information about decimal places is lost. Hence when your main() method prints the double it received you get more decimal places than you intended. (println(), unlike printf(), uses a default formatting which is wider than you want.)

    -----

    What is the getEstimate() method supposed to do?

    I ask because the name suggests it calculates the estimate, but you have it doing other things (formatting to create a string, printing that string.) I suggest you read whatever instructions came with the exercise and don't try and do more in this method than you have to.

    Secondly, the method returns a double. Not a String. The reasons given before would indicate that this getEstimate() method is not the place to be doing the formatting. (formatting can only make numeric values less accurate). If getEstimate() is supposed to return a double, then let it return that double and have main() do the formatting. If getEstimate() itself is supposed to do the formatting, then there is no choice: it should return a String, not a double. The instructions should make it clear which is intended.

    -----

    [Edit] I realise I didn't address the "please guide me" part of your post. You already know how to format numeric quantities - in fact your code illustrates two different methods of doing that.
    Last edited by pbrockway2; March 8th, 2012 at 01:38 AM.

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    84
    My Mood
    Daring
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: how to format double value?

    Quote Originally Posted by pbrockway2 View Post
    I realise I didn't address the "please guide me" part of your post. You already know how to format numeric quantities - in fact your code illustrates two different methods of doing that.
    indeed, my code does, still it i'm not getting formatted output.(please refer to image provided)
    First to explain what is going on...
    please run my code and then compare output with the image(http://www.javaprogrammingforums.com...-execution-jpg ) you will understand better what I want to tell!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,916
    Thanks
    55
    Thanked 2,351 Times in 2,322 Posts

    Default Re: how to format double value?

    Can you copy and paste here your program's output. No images because you can not copy and paste from an image.
    and post what you want the output to look like.

  5. #5
    Member
    Join Date
    Mar 2011
    Posts
    84
    My Mood
    Daring
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: how to format double value?

    here is what my actual output:
    [output]C:\Documents and Settings\Administrator\Desktop\java>javac GroceryPlan.java

    C:\Documents and Settings\Administrator\Desktop\java>java GroceryPlan
    total amount : 12810.60(this and the next row is used for debugging only not need in output)
    Months :12.0
    1174.3 (this is what i get and only this value is required)
    [/output](used for highlighting only)

    and here is what i suppose to have as output
    [expected output] 1147.31 [/expected output]

    i just hope you can understand my problem.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,916
    Thanks
    55
    Thanked 2,351 Times in 2,322 Posts

    Default Re: how to format double value?

    What variable's contents is being printed?
    What is the unformatted value of that variable?

    Your weakly formatted code is hard to read. The code within methods should be indented to make it stand out.

  7. #7
    Member
    Join Date
    Mar 2011
    Posts
    84
    My Mood
    Daring
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: how to format double value?

    Quote Originally Posted by Norm View Post
    What variable's contents is being printed?
    What is the unformatted value of that variable?
    "total" is the variable which resides in getEstimate() called from main.
    first line is given by "printf" statement.(resides in getEstimate() method)
    next line is shown by "println" statement (also in getEstimate() method)
    third line which is only needed is given by "println" statement in method main().

    Your weakly formatted code is hard to read. The code within methods should be indented to make it stand out.
    sorry for this, code is now formatted.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,916
    Thanks
    55
    Thanked 2,351 Times in 2,322 Posts

    Default Re: how to format double value?

    What is the unformatted value of total?

  9. #9
    Member
    Join Date
    Mar 2011
    Posts
    84
    My Mood
    Daring
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: how to format double value?

    1174.30499999998(which is the checking software reports) which must be rounded to 1174.31 i.e. upto two decimal points (required)
    also if i replace
    "Form.format(total)" with just "total" then same value can be seen in output.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,916
    Thanks
    55
    Thanked 2,351 Times in 2,322 Posts

    Default Re: how to format double value?

    What are your rounding rules?
    .304 does not round to .31. Less than .305 should truncate to .3
    You would have to add .005 to get it to round up

  11. #11
    Member
    Join Date
    Mar 2011
    Posts
    84
    My Mood
    Daring
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: how to format double value?

    Is this only solution to this problem?
    well my value is 1174.304999999998 can be rounded to 1174.31 according to mathematics as if we try to remove any 9 then left digit to its right is increased by one, so then the value can become 1174.31.
    yes that worked can you please tell me reason behind that? i insist

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,916
    Thanks
    55
    Thanked 2,351 Times in 2,322 Posts

    Default Re: how to format double value?

    If you want the rounding rules for .4 to round to it 1 then that is your business.
    What value would truncate to 0?
    Just add .5 to it and use normal rounding.
    Normally .4 truncates to 0.

  13. The Following User Says Thank You to Norm For This Useful Post:

    arvindbis (March 10th, 2012)

  14. #13
    Member
    Join Date
    Mar 2011
    Posts
    84
    My Mood
    Daring
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default Re: how to format double value?

    what i'm thinking is if round off that 9 then .304 will become .305 and eventually .31 for up two digits after (dot) decimal point.
    well i'm glad that you have taken so much pain. thank you very much

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,916
    Thanks
    55
    Thanked 2,351 Times in 2,322 Posts

    Default Re: how to format double value?

    .49 rounds to 0
    .5 rounds to 1

Similar Threads

  1. [SOLVED] Read double from console without having to read a string and converting it to double.
    By Lord Voldemort in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 26th, 2011, 09:08 AM
  2. [SOLVED] URGENT - Format a double?
    By macko in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 20th, 2011, 11:46 AM
  3. Java program to format a double value to 2 decimal places
    By JavaPF in forum Java Programming Tutorials
    Replies: 3
    Last Post: December 4th, 2010, 04:08 PM
  4. Java program to format a double value to 2 decimal places
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 3
    Last Post: December 4th, 2010, 04:08 PM
  5. How to format?
    By maximus20895 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 15th, 2010, 02:07 PM