I\'m trying to do some basic math and it keeps popping up as 0
. I\'m sure it has to do with it being an int
but I don\'t know how to work around it
That's integer division.
To get non-integer results, use double
s instead.
Your result is being cast as an int
, so you are losing precision.
Try
double exp1 = 14/20.0*100;
You can change it to 14*100/20 - and then it will give what you want.
I.e. change the sequence of operations (14/20 is 0)
This is not special to blackberry, it's standard java behaviour.
This is because you're doing integer math:
int subexpr1 = 14 / 20; // 0
int subexpr2 = subexpr1 * 100; // 0
Use a double instead or change the order
int expr1 = (int) 14.0/20 * 100; // Very small possibility of rounding errors
int expr2 = 14 * 100 / 20; // Will ignore fraction parts