關於php的number_format函數的有關問題

來源:互聯網
上載者:User
關於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完了以後,接著將__標識去掉
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.