This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
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).]
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
posted
0
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
posted
0
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 very much Mr Manfred to help us. It works fine. Rashid
rani bedi
Ranch Hand
Joined: Feb 06, 2001
Posts: 358
posted
0
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
posted
0
Thank you. Thank you very much. Rashid
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.