Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mistake in Calculation of Percentage

 
Rashid Ali
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1012
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
rani bedi
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
 
Rashid Ali
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rashid,
Look at java.text.DecimalFormat

Regards,
Manfred.
 
Rashid Ali
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much Mr Manfred to help us.
It works fine.
Rashid
 
rani bedi
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. Thank you very much.
Rashid
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic