PHP: Problems with Long Integers & Scientific Notation

Posted by Jetlogs @ 11:44 am
Category: PHP, Web Development

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 »

7 Comments to “PHP: Problems with Long Integers & Scientific Notation”

  1. scultisfalt
    1

    Hello there.
    Just found your site. Great job!
    I like it much.
    look here http://live.com

  2. Herve Kabla
    2

    And what about scanning long integers? %d does not work, and %ll is not recognised!

  3. Jetlogs
    3
    Author Comment

    If scanning, do you mean through POST and GET?

    This article is more about output rather than input. Are you going to do computations with those integers? If not, you can just parse them as string.

  4. Demolitions
    4

    I have quite the opposite problem, i fetch from a DB a string, which contains a number in the format “5,69656893892803000000E+14″, and i need to computate them, at least compare them, tried typecasting and various conversions, but PHP always treat them as they don’t have the “E+nn” part.
    Do you have some advice? (i have already changed ‘,’ in ‘.’ before typecasting)

  5. Jetlogs
    5
    Author Comment

    A simple typecasting to float should work if you wanted to do computations or numeric comparisons. Have you already tried this?:

    $x = "5.69656893892803000000E+14";
    $y = "5.69756893892803000000E+14";
    
    $x_float = (float)$x;
    $y_float = (float)$y;
    
    var_dump($x_float < $y_float); //true
    var_dump($x_float > $y_float); //false
  6. Demolitions
    6

    yes, typecasting wasn’t working, but i finally found the error, i was thinking that the users who entered the data were not fluorescent monkeys, they seem to cannot enter two numbers in the same format, i changed the insertion form and handled the numbers there, now i have correct numbers in the DB, and PHP computes them correctly XD thank you very much for the advice.

  7. cvasilak
    7

    Thanks a lot, worked like a charm!

    Regards,
    Christos

RSS Comment Feed Comments RSS |trackback TrackBack URI

Leave a comment

  • Archives

  • Donations

  • Social Bookmarks

  • Jetlogs.org
    Some Rights Reserved 2007
    Creative Commons License