I’m cycling through an array and adding up a figure.
I have a value of -1,400 in my array. All other values work (i.e. if it is all positive and if it doesn’t have a , in it), but for some reason the following code interprets the -1,400 as "-1".
$totregcat = 0;
while($v=mysql_fetch_array($listreg)) {
print $v['amount']."-";
$totregcat = $v['amount']+$totregcat;
print $totregcat."<br/>;
$regtagid = $v['tagid'];
}
When I print "$v['amount']" I get the right figure, but the adding of it to the variable does not seem to work.
Try
(float) str_replace(",", "", $v['amount'])+$totregcat. That should remove the comma and cast the result as a float.