PHP - 将双精度数与字符串进行比较很奇怪?

发布于 2024-11-08 16:07:27 字数 698 浏览 0 评论 0原文

当将双精度数与字符串进行比较时,我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

谁对谁错谁最难过 2024-11-15 16:07:27

您正在比较浮点。

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:

never compare floating point numbers for equality.

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.

奶气 2024-11-15 16:07:27

在 PHP 中比较值和类型,需要使用 3 个“=”。 like :

$num = 333
$num === 333 => true
$num === '333' => false

请参阅此处了解更多详细信息 http://php.net/manual/en/ language.operators.comparison.php

比较 Float 的一种可能方法是使用 php.net 关于 floats

<?php 
$number1=number_format($float1,2,'.',''); 
$number2=number_format($float2,2,'.'''); 
if($number1!=$number2){ 
 echo 'do correction here!'; 
} 
?>

但显然,没有一个明确的、最好的方法(或者我没有找到)。有些将浮点数转换为字符串,有些则将我刚刚编写的代码转换为字符串。

随你便 ;)

For comparing value AND type in PHP, you need to use 3 "=". like :

$num = 333
$num === 333 => true
$num === '333' => false

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 :

<?php 
$number1=number_format($float1,2,'.',''); 
$number2=number_format($float2,2,'.'''); 
if($number1!=$number2){ 
 echo 'do correction here!'; 
} 
?>

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 ;)

心房敞 2024-11-15 16:07:27

要点:使用 === 而不是 == 来避免类型强制。

原因是在第一个实例中 $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.

烟雨凡馨 2024-11-15 16:07:27

要比较两个 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

流云如水 2024-11-15 16:07:27

您正在比较一个尾随数字有问题的浮点数。

一个认为你可以做的是将浮点数转换为字符串并获取前 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文