I have two integer values, x and total. I am trying to find the percentage of x in total as an integer. This is how I am doing it right now:
percentage = (int)((x*100)/total);
The percentage must be an integer. When I do this it always rounds the decimal point down. Is there a simple way to calculate the percentage as an integer so it rounds up if the decimal is .5 or higher?
Use
Math.round(x * 100.0/total). But note that this returns a long, so a simple cast tointwill be required.I put 100.0 to force it to use floating point arithmetic prior to the rounding.