开发者

Android Java percentage calculation

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 t开发者_StackOverflow中文版he 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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜