美文网首页
PHP 浮点数比较问题

PHP 浮点数比较问题

作者: 许一沐 | 来源:发表于2020-05-28 20:15 被阅读0次

PHP中涉及到数字比较一定要用 bcmath扩展,因为你不知道哪处会有坑,
看代码说明

$price = 0.6 * 2 + 0.6;
        $fee = "0.60";
        $rp = $price - $fee;
        $minCost = "1.20";

        dump([
            "\$price={$price}",
            "\$price's type is " . gettype($price),
            "\$fee={$fee}",
            "\$fee's type is " . gettype($fee),
            "\$rp={$rp}",
            "\$rp's type is " . gettype($rp),
            "\$minCost={$minCost}",
            "\$minCost's type is " . gettype($minCost),
            '"($rp >= $minCost) = " . intval($rp >= $minCost)',
            "($rp >= $minCost) = " . intval($rp >= $minCost),
        ]);

        $price = 1.8;
        $fee = "0.60";
        $rp = $price - $fee;
        $minCost = "1.20";

        dump([
            "\$price={$price}",
            "\$price's type is " . gettype($price),
            "\$fee={$fee}",
            "\$fee's type is " . gettype($fee),
            "\$rp={$rp}",
            "\$rp's type is " . gettype($rp),
            "\$minCost={$minCost}",
            "\$minCost's type is " . gettype($minCost),
            '"($rp >= $minCost) = " . intval($rp >= $minCost)',
            "($rp >= $minCost) = " . intval($rp >= $minCost),
        ]);

        exit;

输出结果:


image.png

相关文章

网友评论

      本文标题:PHP 浮点数比较问题

      本文链接:https://www.haomeiwen.com/subject/yohzahtx.html