php 比较浮点数 - 相同的数字,但不等于?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8364189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:32:02  来源:igfitidea点击:

Comparing floats - same number, but does not equal?

phpfloating-point

提问by jeffkee

Possible Duplicate:
How should I do floating point comparison?
php integer and float comparison mismatch

可能的重复:
我应该如何进行浮点比较?
php整数和浮点比较不匹配

I have two variables, $_REQUEST['amount']and $carttotal, on an e-commerce thing. They of course should match when attempting to process a payment, so as to prevent a manual override of the payment amount at the last minute, or of course, a calculation error.

在电子商务方面,我有两个变量$_REQUEST['amount']$carttotal。在尝试处理付款时,它们当然应该匹配,以防止在最后一分钟手动覆盖付款金额,或者当然,计算错误。

However:

然而:

$carttotal = $carttotal * 1;
$_REQUEST['amount'] = $_REQUEST['amount'] * 1;

if($carttotal != $_REQUEST['amount']) {
    $code = 0; // cart empty under this user - cannot process payment!!! 
    $message = 'The cart total of ' . $carttotal . ' does not match ' . $_REQUEST['amount'] . '. Cannot process payment.';
    $amount = $carttotal;
    $json = array('code' => $code,
                  'message' => $message,
                  'amount' => $amount);
    die(json_encode($json));
} else {
    $trnOrderNumber = $client->id . '-' . $carttotal;
}

The above code, with the same numbers passed, is NOT giving me the equal. Basically I get the error message as if the $carttotal != $_REQUEST['amount']is true(unequal vars).

上面的代码,通过相同的数字,并没有给我相等的。基本上我得到的错误信息就好像$carttotal != $_REQUEST['amount']true(不等的变量)。

So to test the vars, I snuck in:

所以为了测试变量,我偷偷地进来:

var_dump($_REQUEST['amount']);
var_dump($carttotal);

To see what is going on (after I do the * 1calculations to make sure they are dealt with as floats, not strings).

看看发生了什么(在我进行* 1计算以确保它们被处理为浮点数,而不是字符串之后)。

I got this back:

我得到了这个:

float(168.57)
float(168.57)

Very very frustrating. What could be causing this?

非常非常令人沮丧。什么可能导致这种情况?

采纳答案by dqhendricks

floating point numbers have limited precision. view the warning about comparing them here:

浮点数的精度有限。在此处查看有关比较它们的警告:

http://php.net/manual/en/language.types.float.php

http://php.net/manual/en/language.types.float.php

回答by Oliver A.

Floating point numbers are NOT 100% accurate! You calculation in PHP may return 10.00000000001 which is NOT equal to 10.

浮点数不是 100% 准确的!您在 PHP 中的计算可能会返回 10.00000000001,它不等于 10。

Use sprintf ( http://php.net/manual/en/function.sprintf.php) to format the floats before you compare them.

在比较浮点数之前,使用 sprintf ( http://php.net/manual/en/function.sprintf.php) 格式化浮点数。

回答by AR.

Instead of multiplying by one use number_format.

使用 number_format 代替乘以 1。

 $carttotal = number_format((int)$carttotal,2,'.','');
 $_REQUEST['amount'] = number_format((int)$_REQUEST['amount'],2,'.','');