This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Mistake in Calculation of Percentage Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Mistake in Calculation of Percentage" Watch "Mistake in Calculation of Percentage" New topic
Author

Mistake in Calculation of Percentage

Rashid Ali
Ranch Hand

Joined: Jan 16, 2001
Posts: 349
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).]
Greg Harris
Ranch Hand

Joined: Apr 12, 2001
Posts: 1012
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).]


what?
rani bedi
Ranch Hand

Joined: Feb 06, 2001
Posts: 358
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;


Cheers,<br />Rani<br />SCJP, SCWCD, SCBCD
Rashid Ali
Ranch Hand

Joined: Jan 16, 2001
Posts: 349
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
Rashid Ali
Ranch Hand

Joined: Jan 16, 2001
Posts: 349
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
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Rashid,
Look at java.text.DecimalFormat

Regards,
Manfred.
Rashid Ali
Ranch Hand

Joined: Jan 16, 2001
Posts: 349
Thanks very much Mr Manfred to help us.
It works fine.
Rashid
rani bedi
Ranch Hand

Joined: Feb 06, 2001
Posts: 358
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));

Rashid Ali
Ranch Hand

Joined: Jan 16, 2001
Posts: 349
Thank you. Thank you very much.
Rashid
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Mistake in Calculation of Percentage
 
Similar Threads
ArrayIndexOutOfBoundException...
Changing array I think and the print statement
getting NullPointerException
Multi dimensional arrays
Help setting up an array and other issues