I have the following code in my project:
int percent = 2;
int count = 10;
int percentagefill = (percent/10)*count;
System.out.println(percentagefill);
Basically what is happening is that, I’m setting two variables, percent and count. I then calculate the percentage fill. For some strange reason the percentage fill is resulting in a 0, when in this case it should be 2. Any ideas why? Thanks in advance.
intdivided byintwill still result inint. In this case:You can read this question for reference. Also, here’s the Java spec where is says that integer division is rounded towards 0. As for the fix, as long as floating point precision does not become an issue, just use
doubleas PaulP.R.O said.