2012-03-01 110 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