2013-10-08 41 views
1

我的代碼:丟失數據時,轉換浮在PHP詮釋

$baseprice=1705000/1.1; 
var_dump($baseprice); 
$parseprice=intval($baseprice); 
var_dump($parseprice); 

結果:

float(1550000) int(1549999) 

正如你所看到的,我失去了1號時,從浮動轉換成int,它在哪裏,這種情況下轉換沒有丟失數據的解決方案,謝謝。

+0

http://floating-point-gui.de/ – 2013-10-08 03:59:50

回答

1

問題是,當caculate浮子,我仍然不知道爲什麼他們有不同的像波紋管

$baseprice= 1705000/1.1 ; // 1550000 

var_dump(decbin($baseprice)); 
var_dump(decbin(1550000)); 

我們可以通過

$baseprice = round($baseprice); 
3

您正在使用將失去精度的浮點數(請參閱this link)。

如果您正在處理貨幣使用整數並以美分,便士,芬尼格或其他方式工作。在顯示之前轉換爲貨幣格式。

3

解決方案將是如下面

$baseprice=1705000/1.1; 

var_dump($baseprice); 

$parseprice=intval(ceil($baseprice)); 

var_dump($parseprice); 

輸出:

浮子(1550000)INT(1550000)

0

解決這個浮到整輪前值,那麼你可以轉換爲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);