0

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

1
  • A value from the oracle db. I think its varchar type. Commented Mar 20, 2014 at 19:30

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

The references helped me understand a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.