Honorable Monks,

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!


In reply to How to get rounded numbers to =100% ?? by alarix

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.