php我正在打印一个显示为-0的数字

时间:2013-10-03 16:15:24

标签: php negative-number

在php中,我显示的是一个数字,如果它是负数,我正在制作红色,否则为绿色:

if ($daysAhead>=0) $class = "ahead";
else $class = "behind";
echo "<span class=\"$class\">$daysAhead</span>";

我有一个显示为绿色的数字,它打印为-0

为什么显示负号?为什么-0&gt; = 0评估为真?

1 个答案:

答案 0 :(得分:1)

我找到了一个讨论负零概念的网页:

http://hype-free.blogspot.com/2008/12/negative-zero-what-is-it.htmlhttp://en.wikipedia.org/wiki/%E2%88%920_(number)

原来我将数字四舍五入到最接近的小数。所以.009.0四舍五入,但也是负面的。

在php floatval(-0.0)==0中也是如此。

这促使我写了一些非常特殊的代码:

if ($daysAhead==0){  
    $daysAhead=0;
}
if ($daysAhead>=0) $class = "ahead";
else $class = "behind";