alarix has asked for the wisdom of the Perl Monks concerning the following question:
I am VERY, VERY new to PERL. I've written approximately 10 scripts all together. I am also not the best at math, which you'll see soon.
Here's some background.
The latest (and by far the largest) script I've tried is an online form-based survey that stores the answers to multiple choice questions to a text file. I'm grabbing the file data with a script and outputting it to another HTML page which shows the number of answers per question-option, and the percentages.
For example, Question 1A had 4 answers. Question 1B had 8 answers. Question 1A has 33.33% and Question 1B has 66.66% of the total answers to Question 1.
Here's the problem.
I can NOT find a way to round off the decimals so that when I run a cross-tabulation, I can prove that 33.33 + 66.66 = 100. Many of the questions produce percentages such that the percentage is n.50000000000 (etc),which skews my expected total of 100. And this is turning my hairs grey!
Clarification: I can get everything to add up to 100 when I use decimals out to 5 digits. But NOT when I try to round off. (Powers that be do not want to look at ugly decimal points).
Here's the math, such as it is:
@N; #array to store denominator for each question for ($j=0; $j<$numPercents; $j++){ $thePercents[$k][$j] = (($theArrays[$k][$j] / $N[$j]) * 100) +; $thePercents[$k][$j] = sprintf "%3.5f", $thePercen +ts[$k][$j]; #I was trying to deal with the n.5 issue but this clearly won't work . +.. $factor = POSIX::floor($thePercents[$k][$j]); if (($thePercents[$k][$j] - .5) < $factor) { $thePercents[$k][$j] = POSIX::floor($th +ePercents[$k][$j]); } else { $thePercents[$k][$j] = POSIX::ceil($the +Percents[$k][$j]); } }
Please help!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to get rounded numbers to =100% ??
by artist (Parson) on May 22, 2003 at 03:29 UTC | |
by alarix (Initiate) on May 22, 2003 at 14:38 UTC | |
|
Re: How to get rounded numbers to =100% ??
by rob_au (Abbot) on May 22, 2003 at 03:09 UTC | |
|
Re: How to get rounded numbers to =100% ??
by Anonymous Monk on May 22, 2003 at 12:17 UTC | |
by alarix (Initiate) on May 22, 2003 at 14:42 UTC | |
|
Re: How to get rounded numbers to =100% ??
by halley (Prior) on May 22, 2003 at 14:08 UTC | |
by hossman (Prior) on May 23, 2003 at 02:05 UTC | |
by alarix (Initiate) on May 22, 2003 at 14:51 UTC |