PHP Double 比较

PHP Double comparision

提问人:Sergej Fomin 提问时间:8/2/2018 最后编辑:Nico HaaseSergej Fomin 更新时间:8/2/2018 访问量:34

问:

PHP版本7.1.7 / Laravel 5.4

dump($this_tour->payments_to_operator_sum());

1012.0

dump(gettype($this_tour->payments_to_operator_sum()));

“双”

dump($this_tour->operator_price, gettype($this_tour->operator_price));

“1012.00”, “字符串”

dump((double)$this_tour->operator_price, gettype((double)$this_tour->opeartor_price);

1012.0,“双倍”

dump($this_tour->payments_to_operator_sum() == (double)$this_tour->operator_price);

为什么会是假的?

我很困惑。

我明白“==”和“===”的区别,我想这与它无关。 当我这样做时:

dump((double)1012.0 == (double)"1012.00"); 

我得到

双重 比较 相等 php-internals

评论


答:

1赞 Sergej Fomin 8/2/2018 #1

当我这样做时:

return $this_tour->payments_to_operator_sum()

(而不是“转储”),我得到了:

1011.9999999999999

所以这就是答案,我想。