I’m trying to get float (18,16) with padding zeros to the right, but this isn’t working because it always ends with a 1 instead of a zero. My guess it’s because I’ve specified it wrong but I’m not sure how to do it correct.
Sample:
12.12234 must become 12.122340000000000 and 1.01 must become 1.01000000000000.
To achieve this I’m using sprintf('%18.016f', $fMyFloat); but this always ends with a 1 instead of a 0. Obviously I’m overseeing something, I just don’t know what. Any help is greatly appreciated.
This test code:
… prints this in my 64-bit computer:
You are facing the well-known problem of floating point precision. Converting integers from base 10 to base 2 is pretty straightforward (and exact) but base 10 floating point numbers cannot always be represented exactly in base 2. This issue isn’t specific to PHP but there’s a warning in the PHP manual:
Your only chance to achieve such precision is to manipulate numbers as strings. PHP bundles two arbitrary precision libraries that can help you when you need to do match with strings: GNU Multiple Precision and BC Math. Here’s a little hack with bcmath:
It this case, though, you can probably use simple string functions: