[英]PHP - string to 64bit number
我正在使用Twitter API。 有些數字(例如Twitter ID)確實很大,例如199693823725682700。
我已經將此數字轉換為字符串格式,現在我需要將其更改為普通的可讀數字,而不是1000xE09,因為我需要從該字符串廣播數字中減去-1。 然后,我還需要將數字作為字符串發送。
總之,在PHP中,如何將字符串(例如,“ 199693823725682700”)更改為另一個字符串“ 199693823725682699”(原始數字-1)?
非常感謝!
如果BCMath不可用(如果有的話,它將是更好的選擇),此函數將遞減存儲為字符串的任意大小的整數。 沒有浮點數或科學計數法插值的處理,僅適用於帶有可選符號的十進制數字字符串。
function decrement_string ($str) {
// 1 and 0 are special cases with this method
if ($str == 1 || $str == 0) return (string) ($str - 1);
// Determine if number is negative
$negative = $str[0] == '-';
// Strip sign and leading zeros
$str = ltrim($str, '0-+');
// Loop characters backwards
for ($i = strlen($str) - 1; $i >= 0; $i--) {
if ($negative) { // Handle negative numbers
if ($str[$i] < 9) {
$str[$i] = $str[$i] + 1;
break;
} else {
$str[$i] = 0;
}
} else { // Handle positive numbers
if ($str[$i]) {
$str[$i] = $str[$i] - 1;
break;
} else {
$str[$i] = 9;
}
}
}
return ($negative ? '-' : '').ltrim($str, '0');
}
當然。
BC Math模塊
函數http://de.php.net/manual/zh/function.bcsub.php
顯然,目前處理php中大整數的唯一方法是使用bcmath
擴展名。 PHP6中計划使用64位整數。
您應該使用PHP嘗試GMP,
這是手冊 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.