I’m trying to use a hardcoded 64bit integer in a string variable.
Simplfied I want to do something like this:
$i = 76561197961384956;
$s = "i = $i";
Which should result in s being:
i = 76561197961384956
This does obviously not work as PHP cast big integers to float, therefore s is:
i = 7.65611979614E+16
While several other methods like casting etc. fail, I found number_format() and use it like this:
$s = "i = " . number_format($i, 0, '.', '');
But this results in s being:
i = 76561197961384960
Looks like an approximation problem, but how to fix this?
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.
A line like:
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.