PHP:转换64位整数字符串 [英] PHP: Converting a 64bit integer to string
问题描述
我试图用一个硬codeD 64位整数,字符串变量。
I'm trying to use a hardcoded 64bit integer in a string variable.
Simplfied我想做的事情是这样的:
Simplfied I want to do something like this:
$i = 76561197961384956;
$s = "i = $i";
应该产生■哪些
之中:
i = 76561197961384956
这也显然不是,因为PHP铸大整数浮动,因此取值
是:
This does obviously not work as PHP cast big integers to float, therefore s
is:
i = 7.65611979614E+16
而其他一些方法,如铸造等失败了,我发现 number_format()
并使用它是这样的:
While several other methods like casting etc. fail, I found number_format()
and use it like this:
$s = "i = " . number_format($i, 0, '.', '');
不过,这将导致取值
之中:
i = 76561197961384960
看起来像一个近似的问题,而是如何解决这一问题?
Looks like an approximation problem, but how to fix this?
推荐答案
您正在失去对分配的precision,不上串转换。如果这个变量的值实际上是很难codeD,你不能改变的,还有什么可以做。
You're losing the precision on the assignment, not on the string conversion. If this variable's value is actually hardcoded, and you can't change that, there's nothing you can do.
这样一行:
$i = 76561197961384956;
将永远失去precision。如果你需要保持整个事情,把它存储到一个字符串,你不能把它作为存储这样一个int,并保持所有的数字。
will always lose precision. If you need to keep the whole thing, store it into a string, you can't store it as an int like that and keep all the digits.
这篇关于PHP:转换64位整数字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!