在php中将float转换为int时丢失的数据

在php中将float转换为int时丢失的数据

问题描述:

我的代码:

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

结果:

float(1550000) int(1549999)

如你所见,我输了1从float转换为int时的数字,在哪里,这种情况下转换没有丢失数据的解决方案,谢谢。

As you can see, i lost 1 number when convert from float to int, where is it,what solution for this case for convert without lost data, Thanks.

问题是当计算浮动时,我仍然不知道为什么它们有不同的类似下方

Problem is when caculate the float, I still dont know why they have different like bellow

$baseprice= 1705000/1.1 ; // 1550000

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

我们可以解决这个问题

$baseprice = round( $baseprice );