Agro Biz
Agro Biz

Reputation: 11

A simple calculation in php.. I'm doing it wrong?

I do not understand php:

 echo -0.01-0.02-0.16+0.01+0.01+0.17; 

result 2.7755575615629E-17
correctly = 0 !

Upvotes: 1

Views: 115

Answers (2)

Mudassar Saiyed
Mudassar Saiyed

Reputation: 1148

The answer you got is accurate answer according to BODMAS rule if you want round up answer than just use round() function of PHP. You will get you answer.

  echo round(-0.01-0.02-0.16+0.01+0.01+0.17, 8); 

Upvotes: 0

Philip
Philip

Reputation: 3799

E-17 really means x 10 ^ (-17).

According to the computer, which suffers from precision errors at the far end of decimal floating point numbers, it is calculating your answer to be 0.00000000000000002775557...

If you don't need that sort of precision, you can force rounding to a certain accuracy:

echo round(-0.01-0.02-0.16+0.01+0.01+0.17, 8); 

Upvotes: 3

Related Questions