views:

109

answers:

2

I am developing a poll using ruby, but the answer that i get is incorrect, i am looking for the answers that will results in 100% but my answers are sometimes 101% or 99% and i am sure is the rounding off of my floats but i just can get them right because without the rounding off the answers are very long floats. my code of calculating the results and displaying the results is

> 
   @general_poll_graph_data = generalsortedoptions.collect do |o|
             option = [keys[count],       (((o.poll_votes.count.to_f)/@general_poll.poll_votes.count.to_f)*100).round_to(1)]
            count = count + 1
            option
   end
+7  A: 

I think the problem is how rounding works in mathematics. For example if you had 3 options each with 1 vote, rounding to the nearest percentage would give you 33%, 33%, 33% which of course adds up to 99%. Which is quite correct.

It's not possible to make it add up to precisely 100% without fudging the data (i.e. in the above case, you could artificially give one option 34%, but this would not be representative of the actual votes.)

My solution: show the percentages to 1 decimal place, and don't worry about it adding up to precisely 100%

DanSingerman
I was actually thinking of doing the same thing
Donald
+1  A: 

This pretty much happens any time you round relative percentages. It's not really wrong so much as approximate. You can coerce the total to 100, and the best way is probably by looking to see what you need to adjust by and taking that amount out of the numbers that were farthest from what they were rounded to, but you still run the risk of distorting the results even more than you're doing already.

You might want to consider displaying your percentages out to one decimal place, for example 0.6% instead of 1%, to generally reduce that distortion.

chaos