2011-08-22 17:35
浏览 485


I want to be able to convert big ints into their full string derivatives.

For example.

$bigint = 9999999999999999999;
$bigint_string = (string) $bigint;


string(7) "1.0e+19"

but i need

string(19) "9999999999999999999"

Please don't tell me that I should just originally set the $bigint value as a string. That is not an option. I really am stuck and don't know if it is even possible?

图片转代码服务由CSDN问答提供 功能建议

我希望能够将大整数转换成完整的字符串派生词。 < p>例如。

  $ bigint = 9999999999999999999; 
 $ bigint_string =(string)$ bigint; 
var_dump($ bigint_string); 


  string(7)“1.0e + 19”



请不要告诉我我 应该最初将$ bigint值设置为字符串。 这不是一种选择。 我真的被卡住了,不知道是否可能?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • douji9518 2011-08-22 17:48

    You actually should ensure that you type what you mean:

    $bigint = 9999999999999999999;

    Is not a PHP integer but float:


    If you would have done

    $bigint = (int) 9999999999999999999;

    You would have set an integer in fact, but it would not be the number you might have expected:


    It's no problem at all to turn that into string as you might have guessed. So take care when you write numbers into code that you actually write what you mean.

    See this line again:

    $bigint = 9999999999999999999;

    try to understand what you have actually written. It's not an integer at all because PHP will turn it into a float. See Example #4 Integer overflow on a 64-bit system in the integer manual page.

    If you need higher precision, checkout GNU Multiple Precision, it might have what you're looking for. However, this won't change how to write numbers in PHP:

    $bigint = gmp_init("9999999999999999999");
    $bigint_string = gmp_strval($bigint);
    var_dump($bigint, $bigint_string);


    resource(4) of type (GMP integer)
    string(19) "9999999999999999999"
    点赞 打赏 评论
  • dssj88098 2011-08-22 17:36

    Use strval().

    echo strval($bigint); // Will output "99999999999999999"

    EDIT: After a slightly better research (> 5000ms), I see that it is impossible to echo numbers to that precision, because PHP cannot save an integer of that size (depends on OS bit system, 32/64). Seems like it's not possible to do it without losing precision.

    点赞 打赏 评论
  • dua27031 2011-08-22 17:38

    Use printf(), it's made for that:

    echo printf("%d", $bigint);
    点赞 打赏 评论
  • dongqiao5573 2013-05-24 04:50

    Just store the value as string and display...!

       $bigint = '9999999999999999999';


    string(19) "9999999999999999999"
    点赞 打赏 评论

相关推荐 更多相似问题