Compare floats in php

后端 未结 16 1643
清歌不尽
清歌不尽 2020-11-22 00:47

I want to compare two floats in PHP, like in this sample code:

$a = 0.17;
$b = 1 - 0.83; //0.17
if($a == $b ){
 echo \'a and b are same\';
}
else {
 echo \'a         


        
16条回答
  •  长情又很酷
    2020-11-22 01:31

    This works for me on PHP 5.3.27.

    $payments_total = 123.45;
    $order_total = 123.45;
    
    if (round($payments_total, 2) != round($order_total, 2)) {
       // they don't match
    }
    

提交回复
热议问题