2

I want to convert a large integer/float number to a string, it may has no sense but i saw that in a problem solving exam.

My question is so simple,

Why this code returns 14226520737620289536 instead of 14226520737620288370 ?

echo sprintf('%0.0f',14226520737620288370);
Moustafa Elkady
  • 670
  • 1
  • 7
  • 17
  • 4
    To start, `14226520737620288370` isn't a float, it's n integer; and it's outside of valid integer range for 32-bit or 64-bit PHP – Mark Baker Nov 14 '17 at 12:48
  • 2
    PHP uses a float to internally store your large number because 32-bit integer is not large enought, before it even is formatted with your sprintf, and float has no exact integer precision at that range – xander Nov 14 '17 at 12:48
  • https://stackoverflow.com/questions/21902817/convert-float-to-plain-string-representation – ScaisEdge Nov 14 '17 at 12:50
  • sorry, i fixed the question to integer – Moustafa Elkady Nov 14 '17 at 12:50
  • Weird...`echo sprintf('%0.0f',14226520737620283);` prints `14226520737620284` – B001ᛦ Nov 14 '17 at 12:51

0 Answers0