无法在民意调查

时间:2017-07-24 23:49:55

标签: java android int number-formatting

我正在尝试使用以下代码在TextView中显示百分比:

int votedTotal_int = pollObject.getList("votedBy").size();
System.out.println("Total votes cast: " + Integer.toString(votedTotal_int));

int value1_int = pollObject.getList("value1Array").size();
double value1_pct = ((value1_int / votedTotal_int) * 100);
String value1_string = String.valueOf(value1_pct);
holder.value1.setText(value1_string + " %");
System.out.println(holder.value1.getText().toString() + " votes");

鉴于value1_int为1,且votedTotal_int为4,打印输出不应该为25.0%吗?那我为什么要得到以下内容呢?

07-24 16:48:21.626 13321-13321/com.test.android I/System.out: Total votes cast: 4
07-24 16:48:21.626 13321-13321/com.test.android I/System.out: 0.0 % votes
07-24 16:48:21.626 13321-13321/com.test.android I/System.out: 0.0 % votes
07-24 16:48:21.626 13321-13321/com.test.android I/System.out: 0.0 % votes
07-24 16:48:21.626 13321-13321/com.test.android I/System.out: 0.0 % votes

2 个答案:

答案 0 :(得分:0)

double value1_pct = (((double)value1_int * 100) / (double)votedTotal_int);

答案 1 :(得分:-1)

更改以下内容..

double value1_pct = ((value1_int / votedTotal_int) * 100);

double value = (((double)value1_int * 100) / ((double)votedTotal_int));

使用value1_int=1votedTotal_int=1000 or 10000 or 100000

运行示例
07-24 17:09:13.904 3491-3491/? D/TEST: 0.1
07-24 17:09:30.688 3589-3589/? D/TEST: 0.01
07-24 17:09:45.366 3685-3685/? D/TEST: 0.001