CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Poll math Help

 



cir
Novice

Apr 29, 2001, 4:22 PM

Post #1 of 4 (262 views)
Poll math Help Can't Post

I'm writing a poll script, and everything works fine except when it calculates the percent.

if($c[2] && $ata[1]){
$percent = int($ata[1] / $c[2]);
}
is the code, $ata[1] is the total votes for this question and $c[2] is the total votes for this poll. When I did it the other way around I got a division by zero error.
Thanks!

http://


wickedxter
User

Apr 30, 2001, 6:58 PM

Post #2 of 4 (248 views)
Re: Poll math Help [In reply to] Can't Post

im not sure if this will work by try this...

if($c[2] && $ata[1]){
$precent_d = $ata[1] / $c[2];
$percent = int($precent_d);
}


GL



Mortimer
journeyman

May 2, 2001, 7:44 PM

Post #3 of 4 (226 views)
Re: Poll math Help [In reply to] Can't Post


Code
my(@c,@ata); 

$ata[1] = 127; # Total votes for this question.
$c[2] = 1682; # Total votes for this poll.

my(@answers) = ( "$ata[1] / $c[2]",
"int( $ata[1] / $c[2] )",
"int($ata[1] / $c[2] * 100)",
"sprintf('%0.0f' , $ata[1] / $c[2] * 100)",
"sprintf('%0.2f' , $ata[1] / $c[2] * 100)" );

for(@answers){
print eval($_) . " = " . eval($_) * $c[2] / 100 . "\n";
}

...take your pick. Let sprintf() do your rounding, which is what you want, isn't it?
But look at what you lose and gain from answers 3 and 4 respectively.

Anyway, I hope this helps.

Dave.



cir
Novice

May 3, 2001, 11:37 AM

Post #4 of 4 (214 views)
Re: Poll math Help [In reply to] Can't Post

Thanks Mortimer! It works great!
I have had a few problems with the poll, however, it has nothing to do with your code. For some reason the voting part of the script overwrites the whole database. I have a top50 program that uses a similar voting script, so I'll probably just use that with a few modifications.
Thanks again!

http://

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives