PHP简单的加法给出错误的结果



谁能解释一下?我正在做简单的计算,甚至这都不起作用!!

<?php
// Your code here!
echo (10.81+7.00) == 17.81 ?'right':'wrong'; //wrong
?>
$x = 10.81 + 7.00;  // which is equal to 17.81
$y = 17.81;
var_dump($x == $y); // is not true

// PHP thinks that 17.81 (coming from addition) is not equal to 17.81. To make it work, use round()

var_dump(round($x, 2) == round($y, 2)); // this is true

阅读更多

number_format()按千位分组格式化数字。设置十进制位数的数目。如果为0,则从返回值中省略十进制分隔符。

$x = 10.81;  // which is equal to 17.81
$y = 7;  // which is equal to 17.81
$z = 17.81;
function num($float,$digit = 2)
{
return number_format(($float),$digit);
}
echo num($x + $y) == num($z) ?'right':'wrong'; //right

这是最适合你的方法。

相关内容

最新更新