note 20428 added to function.round

From: Date: Wed, 03 Apr 2002 17:16:43 +0000
Subject: note 20428 added to function.round
Groups: php.notes 
Request: Send a blank email to php-notes+get-28545@lists.php.net to get a copy of this message
Round syntax is a MUST in any kind off poll i would say. Many polls on the net have bugs in it, they offendly ouput percents that aren´t equal to 100% as they should be. Example: You have 2002 votes in one poll and there are 3 answears Let´s take a poll that has their percent´s got by $votes = @mysql_query("SELECT SUM(votes) AS Sum FROM ko_answears WHERE votesID = '".$this->Spurning['ID']."' LIMIT 1"); $votes = @mysql_fetch_array($votes); while($arr = @mysql_fetch_array($this->Svor)) { $percents = ($arr['Sum'] ? intval($arr['answears'] / $atkvaedi['Sum'] * 100) : 0 ); answear 1 have 39% or 456 votes answear 2 have 22% or 791 votes answear 3 have 37% or 755 votes 39 plus 22 plus 37 are not 100% so i´m doing something wrong here... But with an Round i can calculate the percents in a right way. You take the SUM of the votes that is 2002 and divide it with 100, then you would have what each and one vote is many percent´s. Then you should multiply each answear with the votes that each answear has: <? $answear1 = (round(22.777222777222777222777222777228, 1)); $answear2 = (round(39.5104895104895104859104895, 1)); $answear3 = (round(37.7122877122877122877122877122877, 1)); $tot = $per1 + $per2 + $per3; echo "$per1 | $per2 | $per3 | total = $tot"; ?> should we take a look at what each variable hold in: $answear1 : 22,8 $answear2 : 39.5 $answear3 : 37.7 $answear3 + $answear2 + $answear3 = 100% voualla! In the end, you don´t output to the client 37.7122877122877122877122877122877 and therfor you have to get the next "hole number" like this would print out 38 or 37,7 with the intval(); syntax but when you do that with all the answears it´s likely to get number that aren´t equal to 100% Use round instead of intval when you calculate results in voting poll scripts Arnór Gauti from Iceland -- http://www.php.net/manual/en/function.round.php http://master.php.net/manage/user-notes.php?action=edit+20428 http://master.php.net/manage/user-notes.php?action=delete+20428 http://master.php.net/manage/user-notes.php?action=reject+20428

« previous php.notes (#28545) next »