I have trouble figuring out why a simple division like this one always returns 0.
System.out.println(4091365376L / 4091495462L * 100L);
I suffixed all numbers with Ls so they’re treated as Longs. I keep getting a zero.
I’m trying to calculate the percentage of 4091365376L to 4091495462L. My values are Longs but I’m just looking for a simple Integer value.
You are a victim of integer rounding (actually: truncation). Try this:
Prints
99. Or cast todoubleimplicitly:Giving
99.99682057572328. Your problem is that4091495462Lis slightly bigger than4091365376Lso when diving them, the result is truncated to0(try3 / 4, if you are not convinced). and0times100is…