Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# Mistake in Calculation of Percentage

Rashid Ali
Ranch Hand
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
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).]

rani bedi
Ranch Hand
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;

Rashid Ali
Ranch Hand
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
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
Posts: 1492
Hi Rashid,
Look at java.text.DecimalFormat

Regards,
Manfred.

Rashid Ali
Ranch Hand
Posts: 349
Thanks very much Mr Manfred to help us.
It works fine.
Rashid

rani bedi
Ranch Hand
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
Posts: 349
Thank you. Thank you very much.
Rashid