could someone explain me what am I doing wrong here, and how can I make this formula to be working? right now it returns 0 in Java and Pawn. But it works in PHP, which isnt making much sense for me.
int test = (65 / 65 / (1 + 25) * 10000);
could someone explain me what am I doing wrong here, and how can I make this formula to be working? right now it returns 0 in Java and Pawn. But it works in PHP, which isnt making much sense for me.
int test = (65 / 65 / (1 + 25) * 10000);
In Java, integer division will truncate the results. For example, 5/2
will truncate 2.5
to 2
.
To ensure you're using float
with numeric constants, add a .0
on the end, as in:
int test = (65.0 / 65.0 / (1.0 + 25.0) * 10000.0);
Java operations for multiplication and division is left to right, so your expression is really:
(((65 / 65) / 26) * 1000) // Clarifying parenthesis
((1 / 26) * 1000)
(0 * 1000) // integer division!
0
To avoid this, you just need to ensure the operations are casted to a double.
The simple way would be just changing the first value to a double, either by:
int test = (65D / 65 / (1 + 25) * 10000);
Or
int test = (65.0 / 65 / (1 + 25) * 10000);
However, if you are refactoring your code later, you might want to change more than just the first value.