变量为负时如何显示0

时间:2015-06-18 13:44:12

标签: javascript jquery

我有一个简单的计算器。我想得到的是当计算值为负时显示0值。

以下是代码:

$("#button").click(function() {
  var total = 0.0;
  $.each($(".amount"), function() {

    var amount = parseFloat($(this).val() || 0);
    var multiply = parseFloat($(".multiply").val());
    if (amount && multiply)
      total += amount * multiply;
  });
  $.each($(".summable"), function() {
    total += parseInt($(this).val() || 0);
  });
  $.each($(".minusable"), function() {
    total -= parseInt($(this).val() || 0);
  });
  $("#sum").val(total.toFixed(2))
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
Amount
<input type="text" class="amount" />
<br>
<select class="multiply">
  <option value="1">One</option>
  <option value="2">Two</option>
  <option value="3">Three</option>
</select>
<br>Sum 1
<input type="text" class="summable" />
<br>Sum 2
<input type="text" class="summable" />
<br>Minus 1
<input type="text" class="minusable" />
<br>Minus 2
<input type="text" class="minusable" />
<br>

<input id="sum" type="text" />
<button id="button">Click Me</button>

只需输入大于金额和总和的减号值。结果会得到负值。请指出当结果值为负时如何显示“0”。

提前致谢。

5 个答案:

答案 0 :(得分:2)

您还可以使用Math.max(),如果其他值为负,则会返回0

$("#sum").val( Math.max( total, 0 ).toFixed(2) );

答案 1 :(得分:1)

添加另一件小事:

if (total.toFixed(2) > 0)
  $("#sum").val(total.toFixed(2));
else
  $("#sum").val("0");

<强>段:

&#13;
&#13;
$("#button").click(function() {
  var total = 0.0;
  $.each($(".amount"), function() {

    var amount = parseFloat($(this).val() || 0);
    var multiply = parseFloat($(".multiply").val());
    if (amount && multiply)
      total += amount * multiply;
  });
  $.each($(".summable"), function() {
    total += parseInt($(this).val() || 0);
  });
  $.each($(".minusable"), function() {
    total -= parseInt($(this).val() || 0);
  });
  if (total.toFixed(2) > 0)
    $("#sum").val(total.toFixed(2));
  else
    $("#sum").val("0");
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
Amount
<input type="text" class="amount" />
<br>
<select class="multiply">
  <option value="1">One</option>
  <option value="2">Two</option>
  <option value="3">Three</option>
</select>
<br>Sum 1
<input type="text" class="summable" />
<br>Sum 2
<input type="text" class="summable" />
<br>Minus 1
<input type="text" class="minusable" />
<br>Minus 2
<input type="text" class="minusable" />
<br>

<input id="sum" type="text" />
<button id="button">Click Me</button>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

这将检查值是否为正,如果不是,则使用0。

$("#sum").val(total > 0 ? total.toFixed(2) : 0);

Ternary Operator

答案 3 :(得分:0)

$("#button").click(function() {
  var total = 0.0;
     $.each($(".amount"), function() {

       var amount = parseFloat($(this).val() || 0);
       var multiply = parseFloat($(".multiply").val());
       if (amount && multiply)
          total += amount * multiply;
     });
    $.each($(".summable"), function() {
       total += parseInt($(this).val() || 0);
    });
    $.each($(".minusable"), function() {
      total -= parseInt($(this).val() || 0);
    });
    if(total < 0){
       total = 0; 
    }
    $("#sum").val(total.toFixed(2))
});

答案 4 :(得分:0)

如果总数小于0,只需检入if,如果是,则将总数设为0。

$("#button").click(function() {
  var total = 0.0;
  $.each($(".amount"), function() {

    var amount = parseFloat($(this).val() || 0);
    var multiply = parseFloat($(".multiply").val());
    if (amount && multiply)
      total += amount * multiply;
  });
  $.each($(".summable"), function() {
    total += parseInt($(this).val() || 0);
  });
  $.each($(".minusable"), function() {
    total -= parseInt($(this).val() || 0);
    if(total<0)
      total=0;
  });
  $("#sum").val(total.toFixed(2))
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
Amount
<input type="text" class="amount" />
<br>
<select class="multiply">
  <option value="1">One</option>
  <option value="2">Two</option>
  <option value="3">Three</option>
</select>
<br>Sum 1
<input type="text" class="summable" />
<br>Sum 2
<input type="text" class="summable" />
<br>Minus 1
<input type="text" class="minusable" />
<br>Minus 2
<input type="text" class="minusable" />
<br>

<input id="sum" type="text" />
<button id="button">Click Me</button>