For those of you working on PHP scripts using very large integers, a heed of warning. It seems that the current version of PHP (5.2.5) apparently has problems working with outputting very large integers and floats that have a lot of lagging 0’s
For example, here is the bug that I have encountered in a server using CentOS:
$big_integer = 1202400000;
echo $big_integer; //outputs 1.2024E+09
$sample_float = (float)100000; //float
echo $sample_float; //outputs 1E+05
As you can see, PHP will automatically convert these types of integers into scientific notation. At first, I though it was simply a case of integer overflow with 32 bit systems, or a misconfiguration in float precision. However, I’ve noticed that this bug will only apply to large integers or floats with a lot of zeros at the end. Apparently, when PHP encounters a number that exceeds the upper limit of 2,147,483,647 for an integer, it automatically converts the number’s type from integer into a double.
Through searching PHP’s bug archives, its seems that I am not alone. Apparently this bug has been documented in the recent versions and is most notable with systems running under CentOS.
So how do we fix this error? Fortunately, we can format these numbers in scientific notation back to their standard integer form using the number_format() function. Here is how to do it:
$big_integer = 1202400000;
$formatted_int = number_format($big_integer, 0, '.', '');
echo $formatted_int; //outputs 1202400000 as expected
$sample_float = (float)100000 //float;
$formatted_float = number_format($sample_float, 2, '.', '');
echo $formatted_float; //outputs 100000.00 as expected
Hope this helps anybody who is experiencing the same problems.








7 Comments 

