I get a list of values from my database and I add them to get the sum of all the rows. PHP is not doing the addition correctly and I can't figure out why. I am working money with what I am doing. The result should be zero, but PHP is calculating the result to be $ -1.78. What is wrong with the following:
public function testAddition()
{
$data = ['23.21','-38.20','14.99','0.00'];
$total = 0;
foreach ($data as $value) {
$total += round(floatval($value), 2);
var_dump($value); // value to add
var_dump($total); // new total
}
// PHPunit results a success to zero, this is wrong, $total = -1.776...
$this->assertEquals(0, $total);
}
Outputs the following:
string(5) "23.21" // value to add
float(23.21) // new total
string(6) "-38.20" // value to add
float(-14.99) // new total
string(5) "14.99" // value to add
float(-1.7763568394003E-15) // <-- this is wrong, should be zero
string(4) "0.00" // value to add
float(-1.7763568394003E-15) // new total