I can't figure out what's wrong with this code... I'm trying to calculate a percentage:
I'm sure that boath npage and numpages are greater than zero (checked it on the emulator's debug variables inspector), but the resulting cCom is always 0:
Double cCom=(double)(npage/numpages);
tpercent.setText("("+cCom.toString()+"%)");
Any ideas?
If npage and numpages are both integers, then Java will round (npage/numpages) to an integer (i.e. 0). To make Java do the division with doubles, you need to cast one of the numbers to a double, like this:
Double cCom = ((double)npage/numpages);
In fact, because you're working with a percentage, you probably want:
Double cCom = ((double)npage/numpages) * 100;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With