I wonder if anyone can shed light on this issue. The best way to describe it is to illustrate it in code snippets as follows.
(PHP 5.3.0, MySQL 5.1.37, XAMPP 1.7.2 on Win XP)
//$report is populated with db rows
$report = $ar->report($this->report_id);
//The db rows are then compiled into total price per branch/cc_name/gl
//$line["price"] contains the value from a decimal(10,2) db field
foreach ($report as $line){
$branch = $this->getBranch($line["cc_name"]);
$lines[$branch][$line["cc_name"]][$line["gl"]] += $line["price"];
}
//DEBUG
foreach ($lines as $branch => $ccs){
foreach ($ccs as $cc_name => $gls){
foreach ($gls as $gl => $amount){
echo $amount.'<br />;
}
}
}
A sample of the results is as follows:
622.77
61.28
584.8299999999999
466.2400000000001
168.73
92.95999999999999
So question 1 is, why is the sum of values from a decimal(10,2) field to so many decimal places.
Now, if I round the amount:
//DEBUG
foreach ($lines as $branch => $ccs){
foreach ($ccs as $cc_name => $gls){
foreach ($gls as $gl => $amount){
$amount = round($amount,2); //Add rounding
echo $amount.'<br />;
}
}
}
I get:
622.77
61.28
584.83
466.24
168.73
92.95999999999999
So question 2 is, Why is the last number is not rounded.