1. Senior Member Join Date
Feb 2011
Posts
259
Rep Power
10

## retrieving percentages

i am trying to get the percentage of votes for each candidate for my program.
votes is an array (with # of votes for each candidate, total is the total number of votes, index shows me how many candidates there are (so i only run the loop "i<index" times), and percents is the initialized array.

Java Code:
`percents = percentages(votes, total, index, percents);`
this is my method to get each of the percents:

Java Code:
```public static int[] percentages(int votes[], int total, int index, int percents[]) {

int i = 0;

for (i=0; i<index; i++) {
}

return percents;

}```
after printing out the percents, i get 0 for each one.  Reply With Quote

2. ## Dividing an int by and int returns an int. I assume that votes[i]/total gets you a number between 0 and 1. If you store that as an int, it's going to truncate and store zero.

You probably want to use a double instead.  Reply With Quote

3. ## Integer division seems to be the culprit here. Since the calculated percent will be a number >=0 && < 1, the decimal will be truncated. To get around integer division you will have to cast one, or both values to a double. You will also have to return a double array.

Java Code:
```int i = 10;
int j = 15
j / i == 1;
(double)j / i == 1.5;
(double)j / (double)i == 1.5;```
edit: too slow :(  Reply With Quote

4. ##  Originally Posted by sunde887 Integer division seems to be the culprit here. Since the calculated percent will be a number >=0 && < 1, the decimal will be truncated. To get around integer division you will have to cast one, or both values to a double. You will also have to return a double array.

Java Code:
```int i = 10;
int j = 15
j / i == 1;
(double)j / i == 1.5;
(double)j / (double)i == 1.5;```
edit: too slow :(
Also, if you're going to be checking for equality, I'd give this a read-through: What Every Computer Scientist Should Know About Floating-Point Arithmetic  Reply With Quote

5. ##  Originally Posted by KevinWorkman Also, if you're going to be checking for equality, I'd give this a read-through: What Every Computer Scientist Should Know About Floating-Point Arithmetic
That looks like a lengthy read. I'll be sure to be checking that out throughout the next few days.

When explaining stuff to people I usually use == to convey equality instead of =, I know = would do fine, but I figure, in case they copy/paste I already used the correct version.  Reply With Quote

6. ##  Originally Posted by sunde887 That looks like a lengthy read. I'll be sure to be checking that out throughout the next few days.
Well, here's the gist:

Floating point arithmetic is rarely exact. While some numbers, such as 0.5, can be exactly represented as a binary (base 2) decimal (since 0.5 equals 2^-1), other numbers, such as 0.1, cannot be. As a result, floating point operations may result in rounding errors, yielding a result that is close to -- but not equal to -- the result you might expect. For example, the simple calculation below results in 2.600000000000001, rather than 2.6:
Java Code:
```  double s=0;

for (int i=0; i<26; i++)
s += 0.1;
System.out.println(s);```
Source: Java performance tuning tips Originally Posted by sunde887 When explaining stuff to people I usually use == to convey equality instead of =, I know = would do fine, but I figure, in case they copy/paste I already used the correct version.
I'm not sure I follow. Single '=' is assignment. Double "==" checks for equality.
Last edited by KevinWorkman; 04-19-2011 at 02:35 PM.  Reply With Quote

7. ## Sorry about the confusion, it just seems natural to use a single = when generally typing, but to programmers you use == so when I'm saying something equals another I just tend to stick to == always now instead. I'm just being confusing. Thanks for the summary of the thread. I hope the op finds all this helpful as well.  Reply With Quote

8. ##  Originally Posted by sunde887 Sorry about the confusion, it just seems natural to use a single = when generally typing, but to programmers you use == so when I'm saying something equals another I just tend to stick to == always now instead. I'm just being confusing. Thanks for the summary of the thread. I hope the op finds all this helpful as well.
Better read that article; chances are higher that'll understand that, say, the following doesn't do what you naively might expect it to do:

Java Code:
```public class T {
public static void main(String[] args) {
for (double x= 0.1; x != 1; x+= 0.1)
System.out.println(x);
}
}```
kind regards,

Jos  Reply With Quote

9. Senior Member Join Date
Feb 2011
Posts
259
Rep Power
10

## here is the data file that i read:

Java Code:
```Smith  80,000
Jones   100,000
Scott   75,000
Washington   110,000
Duffy   125,000
Jacobs   67,000```
i get the total of all the numbers, then take each number, and divide by the total to get my percentage. i am getting 0 for some reason. i guess my array is empty?  Reply With Quote

10. ## A percentage will result in a number between 0 and 1. It's been explained that you should use a double instead of an int. Also don't forget that double values must have a 1.0 to produce the decimal.

Also the article posted is worth the read so your code does not have miscalculations.  Reply With Quote

11. Senior Member Join Date
Feb 2011
Posts
259
Rep Power
10

## so i updated my method to doubles, and here is what i have:

Java Code:
```public static double[] percentages(int votes[], int total, int index, double percents[]) {

int i = 0;

for (i=0; i<index; i++) {
}

return percents;

}```
i still get 0's though.  Reply With Quote

12. ##  Originally Posted by droidus so i updated my method to doubles, and here is what i have:

Java Code:
```public static double[] percentages(int votes[], int total, int index, double percents[]) {

int i = 0;

for (i=0; i<index; i++) {
}

return percents;

}```
i still get 0's though.
votes[ i ] and total are still int typed values so the division will be an int divistion. You did read your text book did you?

kind regards,

Jos  Reply With Quote

13. ## My first post explains the problem, and shows you the change to make. Just in case, you should be casting to double.  Reply With Quote

14. Senior Member Join Date
Feb 2011
Posts
259
Rep Power
10

## ok. so i have a bunch of 0's after the decimal when i print out the values now. how do i get rid of these? can i use printf?  Reply With Quote

15. ##  Originally Posted by droidus ok. so i have a bunch of 0's after the decimal when i print out the values now. how do i get rid of these? can i use printf?
What happened when you tried? Also look into DecimalFormat.  Reply With Quote

16. Senior Member Join Date
Feb 2011
Posts
259
Rep Power
10

## got it: System.out.printf("%,.0f", total);  Reply With Quote

17. ##   Reply With Quote