I'm wanting to convert some numbers into a percentage of each other. The following code works some times but every so often it'll be off by a decimal place.
The problem is with the round(); function. From the example below you will see the percentage values add up to 100.01. This breaks my pie chart. :(
How may I fix this? So the corresponding percentage values will always hit 100 when added together.
$total['one']['value'] = '158';
$total['two']['value'] = '129';
$total['three']['value'] = '121';
$total['all'] = $total['one']['value'] + $total['two']['value'] + $total['three']['value'];
$total['one']['percent'] = round(($total['one']['value'] / $total['all']) * 100, 2);
$total['two']['percent'] = round(($total['two']['value'] / $total['all']) * 100, 2);
$total['three']['percent'] = round(($total['three']['value'] / $total['all']) * 100, 2);
Returns:
Array
(
    [one] => Array
        (
            [value] => 158
            [percent] => 38.73
        )
    [two] => Array
        (
            [value] => 129
            [percent] => 31.62
        )
    [three] => Array
        (
            [value] => 121
            [percent] => 29.66
        )
    [all] => 408
)
 
    