2013-10-03 36 views
0

在PHP中,我展示了一些,我做的紅色如果是負數,綠色,否則即會顯示:PHP我打印數量爲-0

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

我有一個數字,顯示爲綠色,並打印爲-0

爲什麼顯示負號?爲什麼-0> = 0評估爲真?

+1

什麼的'$ daysAhead'的可能值,你的情況 –

+0

圓潤浮點 – ajon

回答

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"; 
+1

我想這應該是你的問題的一部分。 –