在php中,我显示的是一个数字,如果它是负数,我正在制作红色,否则为绿色:
if ($daysAhead>=0) $class = "ahead";
else $class = "behind";
echo "<span class=\"$class\">$daysAhead</span>";
我有一个显示为绿色的数字,它打印为-0
。
为什么显示负号?为什么-0&gt; = 0评估为真?
答案 0 :(得分:1)
我找到了一个讨论负零概念的网页:
http://hype-free.blogspot.com/2008/12/negative-zero-what-is-it.html 和 http://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";