0

I want to create a simple voting program with php, but I get a problem in rounding out the results of the voting.

I have some values ​​from each of the existing topics, for example:

    $TotalVote = 13;

    $Value1 = 6;

    $Value2 = 4;

    $Value3 = 1;

    $Value4 = 1;

    $Value5 = 1;

    Echo 'Value 1:'. $Result1 = round ($value1 / $totalVote * 100);
    Echo '<br>';
    Echo 'Value 2:'. $Result2 = round ($value2 / $totalVote * 100);
    Echo '<br>';
    Echo 'Value 3:'. $Result3 = round ($value3 / $totalVote * 100);
    Echo '<br>';
    Echo 'Value 4:'. $Result4 = round ($value4 / $totalVote * 100);
    Echo '<br>';
    Echo 'Value 5:'. $Result5 = round ($value5 / $totalVote * 100);
    Echo '<br>';
    Echo 'Total Value:'. $TotalResult = $result1 + $result2 + $result3 + $result4 + $result5;`

After I add up all the rounding results, I get 101% results. I ever try to using ceil, but I get 102% resuluts.

How do I get 100% of the overall value exactly when everything is summed up?

Titus Sutio Fanpula
  • 3,467
  • 4
  • 14
  • 33
  • your code will never work because wrong variable spellings and spaces between `$` and variable name like `$ result1 ` is invalid and `$ Value1 !==$ value1` and so on for others .Please correct it – Alive to die - Anant Jun 01 '17 at 11:07
  • you basically have this problem: https://stackoverflow.com/a/6844441/1595977 . As a solution try to restrict to use 2 decimals. – Edwin Jun 01 '17 at 11:11
  • if your values change then result may vary like some time you will get 115 also (for `$Value2 = 6;`). so what you want in that condition? again 100 as a final output? – Alive to die - Anant Jun 01 '17 at 11:12
  • I want to see a percentage of each value and when everything is summed, the result is 100%. – Titus Sutio Fanpula Jun 01 '17 at 12:06

3 Answers3

1

That is what you get for rounding. Your logic makes no sense. Calling something the total value does not make it the total value, it's just the sum of the rounded values :-)

3 voters with values unique values gives 33% rounded for each group. That makes 99% total. Ceil() would give 34*3 = 102.

Arjen
  • 416
  • 4
  • 12
1

You can try this code

$values = [6,4,1,1,1];
$total_vote = array_sum($values);
$total_result = 0;

foreach ($values as $index => $value) {
    $result = $value / $total_vote * 100;
    $total_result += $result;
    echo 'Value ' . ($index + 1) . ': ' . round($result) . '<br/>';
}

echo 'Total Value: ' . round($total_result);

And this code will produce the following output

Value 1: 46
Value 2: 31
Value 3: 8
Value 4: 8
Value 5: 8
Total Value: 100
UfguFugullu
  • 2,107
  • 13
  • 18
  • But the values are not right in theory. You could change the output in the `foreach` into `echo 'Value ' . ($index + 1) . ': ' . round($result, 2) . '
    ';` If it helped you in any way than mark it please.
    – UfguFugullu Jun 01 '17 at 11:29
0

I Edited this code defenitly work for you

<?php
 $totalVote = 13;

$value1 = 6;

$value2 = 4;

$value3 = 1;

$value4 = 1;

$value5 = 1;

Echo 'Value 1:'. $Result1 = ($value1 / $totalVote * 100);
Echo '<br>';
Echo 'Value 2:'. $Result2 = ($value2 / $totalVote * 100);
Echo '<br>';
Echo 'Value 3:'. $Result3 = ($value3 / $totalVote * 100);
Echo '<br>';
Echo 'Value 4:'. $Result4 = ($value4 / $totalVote * 100);
Echo '<br>';
Echo 'Value 5:'. $Result5 = ($value5 / $totalVote * 100);
Echo '<br>';
Echo 'Total Value:'. $totalVote = $Result1 + $Result2 + $Result3 + $Result4 + $Result5;
?>