在 PHP 中比較浮點值
若要在 PHP 中比較浮點值,程式碼如下 −
示例
<?php $a = 2.5; $b = 3.5; echo $a; echo "
$b"; if($a == $b) { echo "
Both the values are equal!"; } else { echo "
Both 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 "
$b"; if((round($a, 2)) == (round($b, 2))) { echo "
Both the values are equal!"; } else { echo "
Both the values aren\'t equal"; } ?>
輸出
這將輸出以下內容 −
1.967 1.969 Both the values are equal!
廣告