2012-03-01 169 views
2

我在使这个公式返回正确的值时遇到了麻烦。根据Steam,等式Steam_community_number = (Last_part_of_steam_id * 2) + 76561197960265728 + Second_to_last_part_of_steam_id应返回64位Steam社区ID。目前,该等式正在返回7.6561198012096E+16。该公式应该返回76561198012095632,这在某种程度上与它已经返回的方式几乎相同。我如何将返回的E + 16值转换为以上代码中所述的正确值?谢谢。PHP数学公式,E + 16?

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; 
} 

我打电话来与$cid = convertSID("STEAM_0:0:25914952");

这个功能如果你想看到的输出的一个例子,检查这里:http://joshua-ferrara.com/hkggateway/sidtester.php

+0

相关:要在大的整数使用bc_math扩展做数学[如何对PHP 64位整数?](HTTP://计算器。 com/questions/864058/how-to-have-64-bit-integer-on-php) – Orbling 2012-03-01 17:18:06

回答

4

变化

return (string)$cid; 

return number_format($cid,0,'.',''); 

请注意,这将返回一个字符串,并且如果您对其执行任何数学运算,它将转换回浮点数。 http://www.php.net/manual/en/book.bc.php

编辑:你的功能转换为使用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; 
} 
+0

有趣的是,非常感谢:) – 2012-03-01 17:33:28

+1

请注意,根据我上面的链接,如果您使用的是64位版本的PHP,你也许可以用普通的操作员来做到这一点。 – Orbling 2012-03-01 17:35:08

+0

这当然是正确的,但是在野外发现一个64b安装的PHP目前并不比寻找白化老虎困难 – Mchl 2012-03-01 17:37:42