PHP - 将双精度数与字符串进行比较很奇怪?
当将双精度数与字符串进行比较时,我在 php 中看到一些奇怪的行为,并希望有人可以向我解释发生了什么。
如果我声明 $num = 0.333; 然后测试 $num=='0.333', 结果证明这是真的。如果我将 $num 加 1,然后减 1,则 $num == '0.333' 结果为 false。如果我随后将 $num 转换为字符串,则比较结果将恢复为 true。它为什么要这样做?
这是一个示例:
<?php
$num = 0.333;
//returns 0.333 double Yes
echo $num, ' ', gettype($num), ' ', $num == '0.333' ? 'Yes' : 'No', '<br />';
$num += 1;
$num = $num - 1;
//returns 0.333 double No
echo $num, ' ', gettype($num), ' ', $num == '0.333' ? 'Yes' : 'No', '<br />';
$str = (string)$num;
//returns 0.333 string Yes
echo $str, ' ', gettype($str), ' ', $str == '0.333' ? 'Yes' : 'No', '<br />';
?>
谢谢。
I'm seeing some weird behavior in php when comparing a double to a string and was hoping someone could explain to me what is going on.
If I declare $num = 0.333;
and then test
$num == '0.333',
this comes out as true. If I then add 1 to $num and then subtract 1, then $num == '0.333' comes out as false. If I then cast $num as a string, the comparison goes back to being true. Why is it doing this?
Here's a sample:
<?php
$num = 0.333;
//returns 0.333 double Yes
echo $num, ' ', gettype($num), ' ', $num == '0.333' ? 'Yes' : 'No', '<br />';
$num += 1;
$num = $num - 1;
//returns 0.333 double No
echo $num, ' ', gettype($num), ' ', $num == '0.333' ? 'Yes' : 'No', '<br />';
$str = (string)$num;
//returns 0.333 string Yes
echo $str, ' ', gettype($str), ' ', $str == '0.333' ? 'Yes' : 'No', '<br />';
?>
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您正在比较浮点。
http://php.net/manual/en/language.types.float.php 说:
==
比较值,但“跨”类型:必须先转换其中一种类型,然后才能实际进行比较。这将导致浮点变量的比较。这就是为什么在执行看似平衡的操作(+1 和 -1)后,您会得到不同的结果。You are comparing a floating point.
http://php.net/manual/en/language.types.float.php says:
The
==
compares for value, but 'across' types: one of the types must be converted before it can actually be compared. And this will result in comparison of floating point variables. That's why after doing a seemingly balanced action (+1 and -1) you're getting different results.在 PHP 中比较值和类型,需要使用 3 个“=”。 like :
请参阅此处了解更多详细信息 http://php.net/manual/en/ language.operators.comparison.php
比较 Float 的一种可能方法是使用 php.net 关于 floats :
但显然,没有一个明确的、最好的方法(或者我没有找到)。有些将浮点数转换为字符串,有些则将我刚刚编写的代码转换为字符串。
随你便 ;)
For comparing value AND type in PHP, you need to use 3 "=". like :
See here for more details http://php.net/manual/en/language.operators.comparison.php
A possible way to compare Float, is to use the method indicated in the comments of php.net regarding floats :
But apparently, there isn't a definitive, best way to do it (or I didn't found it). Some convert the float to String, other does the code I just wrote.
As you like ;)
要点:使用 === 而不是 == 来避免类型强制。
原因是在第一个实例中 $num 是一个 double,但它也等于字符串 '0.333'。
使用 === 显示双精度 0.333 与字符串“0.333”不同。
第二个做了一些加法,现在 double 不再是 0.333,因此它与浮点不准确的字符串不同。
第三个将 0.333 转换为字符串,当然与字符串相同。
Take away point: use === instead of == to avoid type coercion.
The reason is that in the first instance $num is a double, but it is also equal to the string '0.333'.
Using === shows that the double 0.333 isn't the same as the string '0.333'.
The second one has done some addition, now the double isn't exactly 0.333 anymore, so it isn't the same as a string to to floating point inaccuracies.
The third one has cast 0.333 to a string, which is of course the same as the string.
要比较两个 float 或 double,请使用 http://php.net/manual/en/function。 bccomp.php
To compare two float or double use http://php.net/manual/en/function.bccomp.php
您正在比较一个尾随数字有问题的浮点数。
一个认为你可以做的是将浮点数转换为字符串并获取前 x 个字符(即,如果你有一个要与之比较的字符串“.333”,则将浮点数转换为字符串并获取前四个字符) ,或者您可以在比较之前将浮点数取整至适当的小数。
You are comparing a float for which trailing digits are a problem.
One think you can do is convert the float to a string and take the first x characters (ie if you have a string '.333' that you're comparing it to, convert the float to a string and take the first four characters), or you can floor the float to the proper decimals before comparing it.