0

I was debugging a bug when I came across a piece of code which can be simplified as below

$value = '9611080820001471'; $value = sprintf('%.0f', (float)$value); echo $value . "\n";

This produces following Output:

9611080820001472

Can somebody explains why/how the $value got incremented

Jakub Matczak
  • 15,341
  • 5
  • 46
  • 64
robert
  • 8,459
  • 9
  • 45
  • 70
  • 3
    Floating point numbers have limited precision. see http://php.net/manual/en/language.types.float.php – Tim Mar 10 '16 at 10:10
  • 2
    Any of the billion-plus answers here on SO dealing with the inaccuracies of floating point would probably help you out here :-) – paxdiablo Mar 10 '16 at 10:11
  • As soon as you do `(float)` you're trading *accuracy* for a larger value range. Don't *expect* any particular result when working with floats. – deceze Mar 10 '16 at 10:19
  • Ohh.. I thought it was something related to sprintf.. – robert Mar 10 '16 at 10:20

0 Answers0