PHP Math Equation, E+16?
-
15-11-2019 - |
Question
I'm having trouble with this equation in getting it to return the proper value. According to Steam, the equation Steam_community_number = (Last_part_of_steam_id * 2) + 76561197960265728 + Second_to_last_part_of_steam_id
Should return the 64 bit Steam Community ID. Currently, this equation is returning 7.6561198012096E+16
. The equation should be returning 76561198012095632
which in a way is almost the same as what it is already returning. How would I convert the returned E+16 value to the correct value as stated above in my code below? Thanks.
function convertSID($steamid) {
if ($steamid == null) { return false; }
//STEAM_X:Y:Z
//W=Z*2+V+Y
//Z, V, Y
//Steam_community_number = (Last_part_of_steam_id * 2) + 76561197960265728 + Second_to_last_part_of_steam_id
if (strpos($steamid, ":1:")) {
$Y = 1;
} else {
$Y = 0;
}
$Z = substr($steamid, 10);
$Z = (int)$Z;
echo "Z: " . $Z . "</br>";
$cid = ($Z * 2) + 76561197960265728 + $Y;
echo "Equation: (" . $Z . " * 2) + 76561197960265728 + " . $Y . "<br/>";
return (string)$cid;
}
And I am calling this function with $cid = convertSID("STEAM_0:0:25914952");
If you would like to see an example of the output, check here: http://joshua-ferrara.com/hkggateway/sidtester.php
Solution
Change
return (string)$cid;
to
return number_format($cid,0,'.','');
Be aware that this will return a string, and if you do any math on it, it will be converted back to float. To do math on large integers use bc_math
extension: http://www.php.net/manual/en/book.bc.php
edit: your function converted to use bcmath:
function convertSID($steamid) {
if ($steamid == null) { return false; }
//STEAM_X:Y:Z
//W=Z*2+V+Y
//Z, V, Y
//Steam_community_number = (Last_part_of_steam_id * 2) + 76561197960265728 + Second_to_last_part_of_steam_id
$steamidExploded = explode(':',$steamid);
$Y = (int)steamidExploded[1];
$Z = (int)steamidExploded[2];
echo "Z: " . $Z . "</br>";
$cid = bcadd('76561197960265728 ',$Z * 2 + $Y);
echo "Equation: (" . $Z . " * 2) + 76561197960265728 + " . $Y . "<br/>";
return $cid;
}