我有一個數據庫存儲車輛的長度爲17個字符的VIN號碼(存儲爲VID)。我正在嘗試增加到下一個VID以將其顯示在可以在數據庫中添加新車輛的頁面上。數據庫中的VID字段輸入bigint(17)並設置了auto_increment。在PHP中有大的雙倍行爲奇怪的行爲MySQL
$incr_query = "SELECT MAX(VID) FROM Vehicle";
$incr_result = mysql_query($incr_query);
$row = mysql_fetch_assoc($incr_result);
$vid = $row["MAX(VID)"] + 1;
輸出:
print var_dump{$row);//array(1) { ["MAX(VID)"]=> string(17) "12345678123456788" }
print gettype($vid); //double
print $vid; //1.2345678123457E+16
print number_format(++$vid, 0, '.', ''); //12345678123456788
print number_format(--$vid, 0, '.', ''); //12345678123456788
print number_format($vid - 5, 0, '.', ''); //12345678123456784
我不知道是怎麼回事。按照PHP float manual:
"The size of a float is platform-dependent, although a maximum of ~1.8e308 with
a precision of roughly 14 decimal digits is a common value"
編輯:
因此,使用bcadd,這些都是輸出:
$vid = bcadd($vid, '1', 0);
print "\n".$vid; //12345678123456789
print "\n".number_format($vid, 0, '.', ''); //12345678123456788
嗯,它不夠準確,看起來?這個數字超過14位數,所以估計似乎適用於你的架構 – 2012-04-05 00:48:54