user3201410
user3201410

Reputation: 21

adding decimal values with php

I have this code:

$sum=0;
while($row=oci_fetch_row($que))
{

$sum+=$row[8];
}

echo number_format((float)$sum,2,",","").'<br>';

there are 3 rows with values '7.01' '43.76' '11.64' echo prints 61.00 instead of 62.41. Why? The corresponding to mySql code works like a charm..... SOLVED..... How to convert a string to float with "tail"? thanks guys for your time

Upvotes: 0

Views: 69

Answers (1)

AbraCadaver
AbraCadaver

Reputation: 78994

Floating point arithmetic. See Floating point precision and Why don’t my numbers add up?

echo serialize(7.01);
echo serialize(43.76);
echo serialize(11.64);;

Try BC Match Functions or round to 2 decimal places first:

echo round(7.01, 2) + round(43.76, 2) + round(11.64, 2);
//$sum += round($row[8], 2);

Upvotes: 1

Related Questions