# how to format double value?

• March 8th, 2012, 12:04 AM
arvindbis
how to format double value?
here is waht I'm tring to do:
Quote:

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
Code :

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
Attachment 1093

Please guide me how to format double values? please look at the screen shot
• March 8th, 2012, 12:34 AM
pbrockway2
Re: how to format double value?
Quote:

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.

-----

 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.
• March 10th, 2012, 10:38 AM
arvindbis
Re: how to format double value?
Quote:

Originally Posted by pbrockway2
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)
Quote:

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!
• March 10th, 2012, 10:41 AM
Norm
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.
• March 10th, 2012, 10:49 AM
arvindbis
Re: how to format double value?
here is what my actual output:

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.
• March 10th, 2012, 10:58 AM
Norm
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.
• March 10th, 2012, 11:11 AM
arvindbis
Re: how to format double value?
Quote:

Originally Posted by Norm
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().

Quote:

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.
• March 10th, 2012, 11:12 AM
Norm
Re: how to format double value?
What is the unformatted value of total?
• March 10th, 2012, 11:40 AM
arvindbis
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.
• March 10th, 2012, 11:52 AM
Norm
Re: how to format double value?
.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
• March 10th, 2012, 12:17 PM
arvindbis
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
• March 10th, 2012, 12:29 PM
Norm
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.
• March 10th, 2012, 12:48 PM
arvindbis
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 :o
• March 10th, 2012, 12:49 PM
Norm
Re: how to format double value?
.49 rounds to 0
.5 rounds to 1