Forums Register Login

Mistake in Calculation of Percentage

+Pie Number of slices to send: Send
What am I missing. All the things are working properly except the Average marks formula.
Please suggest my mistake.
Thanks
Rashid

[This message has been edited by Rashid Ali (edited June 27, 2001).]
+Pie Number of slices to send: Send
rashid,
there might be another way to fix your problem, but i got it to work by changing these lines...
int obtainMarks, totalMarks = 850 ;
to
double obtainMarks, totalMarks = 850 ;
you need the totals to be doubles so the decimal points will not be dropped when you calculate the average. the int was dropping the decimals so your calculation was returning 0
and you have to take the "%" out of the avgMarks calculation because you cannot include a string with the math... you also need to reverse totalMarks / obtainMarks to get the math to work correctly. my line looks like this:
avgMarks = ( ( obtainMarks / totalMarks ) * 100 );
this calculates the percentage with about 8 decimal points... there is a way to set the precision (2 decimal places) but i cannot think of it right now.
hope this helps!
greg
[This message has been edited by Greg Harris (edited June 27, 2001).]
+Pie Number of slices to send: Send
avgMarks =( ( ( totalMarks / obtainMarks ) * 100 )+ "%" ) ;
In the above statement you have interchanged the positions of variables totalMarks and obtainMarks. It may be typing mistake even.
avgMarks =(((obtainMarks / totalMarks) * 100 )) ;
The variables totalMarks, obtainMarks are declared as int.
When you are dividing an int by int it may not result in whole numbers. Thus you may loose decimal part since the result should be an int.
So u need to do any of the following things in your code--
either declare totalMarks, obtainMarks as double
double totalMarks, obtainMarks;
or break the statement into two parts
double d = obtainMarks / totalMarks;
avgMarks = d*100;
+Pie Number of slices to send: Send
Thanks very much to both of you.
Now my programs works fine. I just changed the type from int to double and changed the command as:
avgMarks = ( ( obtainMarks / totalMarks ) * 100 );
And it works fine. In the start of my program i applied the same statement but due to the int type it did not show the result and i had to write many statment which complicated this statment.
Thanks again for your quick and effective responses .
Best regards
Rashid
THANK U JAVARANCH TOO
+Pie Number of slices to send: Send
As Quoted from Greg Harris reply:


...this calculates the percentage with about 8 decimal points... there is a way to set the precision (2 decimal places) but i cannot think of it right now.


My program shows the result in 8 decimal points as mentioned above. It would be nice if someone can let me know how to reduce or set the precision upto 2 decimal places.

Thanks
~Rashid
+Pie Number of slices to send: Send
Hi Rashid,
Look at java.text.DecimalFormat

Regards,
Manfred.
+Pie Number of slices to send: Send
Thanks very much Mr Manfred to help us.
It works fine.
Rashid
+Pie Number of slices to send: Send
you can even use NumberFormat class
NumberFormat fmt = NumberFormat.getInstance();
fmt.setMaximumFractionDigits(2);
float f = 100.28f;
System.out.println("As a float : " + f);
double d = f;
System.out.println("Cast to a double : " + d);
System.out.println("Using NumberFormat: " + fmt.format(d));

+Pie Number of slices to send: Send
Thank you. Thank you very much.
Rashid
But how did the elephant get like that? What did you do? I think all we can do now is read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 9902 times.
Similar Threads
Changing array I think and the print statement
getting NullPointerException
Multi dimensional arrays
Help setting up an array and other issues
ArrayIndexOutOfBoundException...
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 09:23:18.