關於php的number_format函數的問題.
echo number_format(88899626759579787, 0, '', '');
為什麼輸出結果是88899626759579792 ?
分享到:
------解決方案--------------------
我也是新手,問了下度娘,她說,格式出錯了!
出自 http://www.w3school.com.cn/php/func_string_number_format.asp
------解決方案--------------------
因為你的環境應該32位,要表示你上述88899626759579787的數字,要用浮點型,而浮點型的精度是不沒有辦法保證的.
可以通過這個例子就知道了
echo PHP_INT_MAX; // php的整形能表示的最大整數
var_dump(number_format(88899626759579700, 0, '', ''));
------解決方案--------------------
有沒有試過按 #1 說的改下看看
------解決方案--------------------
越界了,php 的 long 類型無法表示那麼大的正數
php 開發組織從未發行過 for win 的 64位 版本
------解決方案--------------------
如果有條件的話,最好能升級到php5.4+,因為php在5.4的時候,對json_decode增加一個參數
引用
5.4.0 The options parameter was added.
Bitmask of JSON decode options. Currently only JSON_BIGINT_AS_STRING is supported (default is to cast large integers as floats)
$json = '12345678901234567890';
var_dump(json_decode($json));
var_dump(json_decode($json, false, 512, JSON_BIGINT_AS_STRING));
?>
以上常式會輸出:
float(1.2345678901235E+19)
string(20) "12345678901234567890"
如果不能升級,建議首先將超過int整形長度的全數字文本首先加一個特殊標識,例如
{"a":"12345678901234567890"}
可以通過正則替換成
{"a":"__12345678901234567890"}
等json_decode完了以後,接著將__標識去掉