[英]how to convert string to int when we read excel data in php?
[英]Lost data when convert float to int in php
我的代碼:
$baseprice=1705000/1.1;
var_dump($baseprice);
$parseprice=intval($baseprice);
var_dump($parseprice);
結果 :
float(1550000) int(1549999)
如您所見,從 float 轉換為 int 時,我丟失了 1 個數字,它在哪里,這種情況下有什么解決方案可以在不丟失數據的情況下進行轉換,謝謝。
您正在使用會失去精度的浮點數(請參閱此鏈接)。
如果您處理貨幣,請使用整數並以美分、便士、芬尼或其他單位工作。 在顯示之前轉換為貨幣格式。
解決方案如下
$baseprice=1705000/1.1;
var_dump($baseprice);
$parseprice=intval(ceil($baseprice));
var_dump($parseprice);
輸出:
浮動(1550000)int(1550000)
問題是當計算浮點數時,我仍然不知道為什么它們像波紋管一樣不同
$baseprice= 1705000/1.1 ; // 1550000
var_dump( decbin( $baseprice ));
var_dump( decbin(1550000));
我們可以通過
$baseprice = round( $baseprice );
在 float 到 integer 四舍五入之前,您可以轉換為 int
$baseprice=1705000/1.1;
var_dump($baseprice);
//Before float to integer round the value then you can convert to int
$baseprice =round($baseprice);
$parseprice=intval($baseprice);
var_dump($parseprice);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.