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.


21 Comments »

21 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

  8. jonas
    8

    > Hope this helps anybody who is experiencing the same problems.

    Indeed. it saved me a lot of time, Thanks

    Greetings
    Jonas

  9. Chad
    9

    Related problem!

    I WANT scientific notation. I am working with some numbers in the 2.08e17 range, and that’s an unacceptable number of digits to show.

    How do I
    A) show only the first 5 non-zero digits of a number and
    B) force scientific notation, if this doesn’t include the decimal point?

  10. tahsin hasan
    10

    nice post, lots of time eaten up this php 5.2.6 bug. thanks for the post.

  11. Dennis Decoene
    11

    Fantastic. I was getting this problem with the twitter api’s next cursor. This saved me a bunch of headaches!

    Dennis

  12. ppp
    12

    Thanks

  13. meraj
    13

    how do i modify the number_format function to get expected result for 899350102000029210 as well.

    it output 899350102000029400 instead of 899350102000029210

    thanks

  14. Sean
    14

    Same problem here, with PHP and the Twitter API. Thanks for the tip!

  15. Trackback:
    15
    Lets discuss Technology!!

    PHP: Problem with Long Integers…

    Recently we faced an issue in PHP with long integers. The integer number was converted to decimal number. Here is the sample output: $big_integer = 1202400000; echo $big_integer; //outputs 1.2024E+09  $sample_float = (float)100000; //float echo $sample…

  16. chris
    16

    Hi,

    Be careful. This seems not work for a very large integer.

    If you have 18446744073709551615, PHP will convert it to 1.84467440737E+19.

    But if you try to convert it back to a normal integer using number_format($big_integer, 0, ‘.’, ”), you’ll get 18446744073709551616 (the original + 1).

  17. Daniel
    17

    Hey,

    I’m getting the same type of problem as Chris and meraj. The converted integer formats to a different number than intended.

    Any idea how to solve this.

    For example, -9223372036854387285 becomes -9223372036854387712
    when I pass it to a function and run number_format on it.

  18. mctaco
    18

    10 thumbs up! was having the problem with Facebook uids! solved

  19. Dint
    19

    Same problem as meraj, Chris and Daniel. I enter 84268019035021313 and the result is 84268019035021312. Any idea yet on how to fix this? Thanks in advance!

  20. Bauke Regnerus
    20

    Same problem here with PHP 5.3.10. The integers are converted to wrong scientific notation. So, 5311693811158833914 becomes 5.3116938111588E+18, which actually is 5311693811158834176. Still looking for a solution, maybe downgrading PHP.

  21. Mafia Inferno
    21

    Regarding the display of long integers . . .

    PHP uses a max of 13 digits after the decimal point when using scientific notation.

    I use a combination of the bc precision Math functions and a custom function to display numbers with commas. The bc Math functions all use strings instead of integers so are more precise and wont be stored using scientific notation.

    a number format with commas . ..
    function commas ($str){
    // integer only, remove everything after decimal point
    $arr = explode(“.”, $str, 1);
    $str = $arr[0];

    // reverse the string so we can split each 3 numbers from RTL
    $str = strrev($str);
    $xploded = str_split($str,3);
    $comma_separated = implode(“,”, $xploded);
    return strrev($comma_separated);
    }

RSS Comment Feed Comments RSS |trackback TrackBack URI

Leave a comment

  • Archives

  • Donations

  • Social Bookmarks

  • Jetlogs.org
    Some Rights Reserved 2007
    Creative Commons License