Results 1 to 17 of 17
Thread: retrieving percentages
 04192011, 01:54 PM #1Senior Member
 Join Date
 Feb 2011
 Posts
 244
 Rep Power
 4
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);
Java Code:public static int[] percentages(int votes[], int total, int index, int percents[]) { int i = 0; for (i=0; i<index; i++) { percents[i] = (votes[i]/total); } return percents; }
 04192011, 02:07 PM #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.How to Ask Questions the Smart Way
Static Void Games  Play indie games, learn from game tutorials and source code, upload your own games!
 04192011, 02:11 PM #3
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 8
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;
 04192011, 02:13 PM #4
Also, if you're going to be checking for equality, I'd give this a readthrough: What Every Computer Scientist Should Know About FloatingPoint Arithmetic
How to Ask Questions the Smart Way
Static Void Games  Play indie games, learn from game tutorials and source code, upload your own games!
 04192011, 02:17 PM #5
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 8
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.
 04192011, 02:27 PM #6
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);
I'm not sure I follow. Single '=' is assignment. Double "==" checks for equality.Last edited by KevinWorkman; 04192011 at 02:35 PM.
How to Ask Questions the Smart Way
Static Void Games  Play indie games, learn from game tutorials and source code, upload your own games!
 04192011, 02:38 PM #7
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 8
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.
 04192011, 02:57 PM #8
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,003
 Blog Entries
 7
 Rep Power
 20
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); } }
Joscenosillicaphobia: the fear for an empty beer glass
 04192011, 07:12 PM #9Senior Member
 Join Date
 Feb 2011
 Posts
 244
 Rep Power
 4
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
 04192011, 07:20 PM #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. Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
 +Rep people for helpful posts.
 04202011, 12:12 PM #11Senior Member
 Join Date
 Feb 2011
 Posts
 244
 Rep Power
 4
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++) { percents[i] = (votes[i]/total); } return percents; }
 04202011, 12:18 PM #12
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,003
 Blog Entries
 7
 Rep Power
 20
 04202011, 12:49 PM #13
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 8
My first post explains the problem, and shows you the change to make. Just in case, you should be casting to double.
 04202011, 02:12 PM #14Senior Member
 Join Date
 Feb 2011
 Posts
 244
 Rep Power
 4
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?
 04202011, 02:15 PM #15How to Ask Questions the Smart Way
Static Void Games  Play indie games, learn from game tutorials and source code, upload your own games!
 04202011, 02:24 PM #16Senior Member
 Join Date
 Feb 2011
 Posts
 244
 Rep Power
 4
got it: System.out.printf("%,.0f", total);
 04202011, 02:26 PM #17
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 8
Similar Threads

Percentages
By Angziety94 in forum New To JavaReplies: 1Last Post: 12092010, 05:14 AM 
using percentages?
By shroomiin in forum New To JavaReplies: 3Last Post: 09302009, 01:58 AM 
[SOLVED] Noobie  Calculating Percentages
By fullmetaljacket in forum New To JavaReplies: 16Last Post: 05222009, 01:10 AM 
Percentages
By Ciwan in forum New To JavaReplies: 3Last Post: 02162009, 05:34 AM 
Qadratic Formula and Percentages
By bbtgirl in forum JCreatorReplies: 4Last Post: 02072009, 03:07 AM
Bookmarks