首页 文章资讯内容详情

PHP中比较浮点值

2026-06-04 1 花语

为了比较PHP中的float值,代码如下-

示例

<?php $a = 2.5; $b = 3.5; echo $a; echo "\n$b"; if($a == $b) { echo "\nBoth the values are equal!"; } else { echo "\nBoth the values aren\t equal"; } ?>

输出结果

2.5 3.5 Both the values aren\t equal

示例

现在让我们来看另一个示例-

<?php $a = 1.967; $b = 1.969; echo $a; echo "\n$b"; if((round($a, 2)) == (round($b, 2))) { echo "\nBoth the values are equal!"; } else { echo "\nBoth the values aren\t equal"; } ?>

输出结果

这将产生以下示例-

1.967 1.969 Both the values are equal!