2016-09-22 78 views
-1

我將如何停止下面的返回小於0的值。我想要小於0的任何東西顯示爲0.任何幫助表示讚賞。Jquery小於0的數學顯示0

$("#spanrPower_CO2").text (Math.round(-data[0] * 16.8) * 100/100); 
+4

'Math.max(0,Math.round(-data [0] * 16.8)* 100/100);' –

+1

你可能會得到更清晰的代碼爲'Math.max您預期的結果(0 ,-data [0] * 16.8).toFixed(2)'。查看http://stackoverflow.com/questions/11832914/round-to-at-most-2-decimal-places-in-javascript – LutzL

+0

@LutzL:比我的回答更好聽。考慮推廣它。 – Bathsheba

回答

2

data[0] > 0 ? 0 : -Math.round(16.8 * data[0]);可能更清楚。

請注意,除非您接近浮點無窮大,否則* 100/100是無操作的。

0
(Math.max(0, -data[0] * 16.8).toFixed(0));