santafebound santafebound - 3 years ago 55
Java Question

Can't display correct percentage value in poll

I'm trying to display a percentage in a TextView with the following code:

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");


Given that value1_int is 1, and votedTotal_int is 4, shouldn't the print out read 25.0%? So why am I getting the following?

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

JRG JRG
Answer Source

Change the below ..

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

to

double value1_pct = ((value1_int * 100) / votedTotal_int);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download