1

I have this PHP code below:

$value = -.9;
for ($i = 0; $i <= 6; $i++) {
    echo "value = $value , ";
    $value += .3;
}

Output:

value = -0.9 , value = -0.6 , value = -0.3 , value = -1.11022302463E-16 , value = 0.3 , value = 0.6 , value = 0.9 , 

Why am I getting -1.11022302463E-16 and not 0? And what can I do to correct the problem? Thanks again.

LennieCodes
  • 733
  • 8
  • 13
  • 1
    because `0.3` and the like cannot be presented exactly as floating point numbers. it's 0.299999999999234524234234 or whatever interally. – Marc B May 23 '14 at 17:17

1 Answers1

1

try this

echo 'value = ' . number_format($value,0,'','').', ';
MH2K9
  • 11,951
  • 7
  • 32
  • 49