PHP Integer Problem

后端 未结 5 1983
一生所求
一生所求 2021-01-14 19:42

When I print this number in php 137582392964679 I get this as the output 1.37582392965E+14

All I am doing is a simple



        
5条回答
  •  执笔经年
    2021-01-14 20:10

    The maximum number you can store in a signed integer on a 32-bit machine is 2147483647. You can store numbers larger than this in a float but you risk losing some precision.

    If that's the case how can I get around this problem?

    You probably want to use a big number library. Try GMP:

    $sum = gmp_add("123456789012345", "76543210987655");
    echo gmp_strval($sum) . "\n";
    

    Result:

    200000000000000
    

    Another alternative you could use is BC Math.

    If you don't need to do any calculations with these numbers, but just store tham correctly, then store them as strings rather than integers.

提交回复
热议问题