Results 1 to 4 of 4
Thread: Calculating percent
 11242012, 08:31 PM #1Member
 Join Date
 Nov 2012
 Posts
 4
 Rep Power
 0
Calculating percent
I have this code:
Java Code:System.out.println(); System.out.println("Rolling dice " + rolls + " times..."); System.out.println("Sum Total Percent"); System.out.println("  "); for (int i = 2; i < combinationCount; i++) { double percent = (combinations[i] / rolls) * 100; if (combinations[i] != 0) { System.out.println(i + "\t " + combinations[i] + "\t\t" + percent); } else { continue; } }

Re: Calculating percent
You appear to be doing int division meaning you may be dividing an int by an int, and this will always return an int, even if it is placed in a double variable (percent). So if your numerator is smaller than your denominator, then this division will always return zero. One solution is to cast either the numerator or denominator as a double:
Java Code:double percent = ((double)combinations[i] / rolls) * 100;
 11242012, 09:00 PM #3Member
 Join Date
 Nov 2012
 Posts
 4
 Rep Power
 0
Re: Calculating percent
That makes sense now, thank you.

Similar Threads

Why is a negative percent not giving me the correct result?
By MXA92 in forum New To JavaReplies: 2Last Post: 10252011, 10:26 PM 
need help with calculating something
By mikec420 in forum New To JavaReplies: 13Last Post: 09292011, 09:14 PM 
passing percent to public int method
By BillyB in forum New To JavaReplies: 3Last Post: 02042011, 06:08 AM 
calculating the hypotenuse
By Latanyar in forum New To JavaReplies: 6Last Post: 10122010, 09:20 AM 
calculating with dates
By hannes in forum New To JavaReplies: 1Last Post: 01142010, 08:22 PM
Bookmarks