Given the fact that a floating point is imprecise and that I should use the the BCMath function to get precise results, is it really necessary to use price strings in PHP and add them up with bcadd when making a 'simple' shopping cart?
Given the following example, why shouldn't I use a rounded float?
I'm looking for a best practice for calculating the order total for a shopping cart.
$floatTotal = 0.00;
$stringTotal = '0.00';
for($i=0; $i<1000; $i++) {
$floatTotal += 0.1;
}
echo "float value: ";
printf('%.40f', $floatTotal); //99.9999999999985931253831950016319751739502
echo "<br>\n";
echo "rounded float value: ". round($floatTotal); //100
echo "<br>\n";
for($i=0; $i<1000; $i++) {
$stringTotal = bcadd($stringTotal, '0.1', 2);
}
echo 'string value: '.$stringTotal.'<br>'; //100.00