如果金额为负,则更改表格行的颜色

时间:2019-04-10 18:18:28

标签: javascript php jquery

尝试更改以下代码中的颜色。

  

如果金额为正(利润)-> 绿色发生这种情况

     

如果金额为负数(亏损)-> 红色doesn't发生

function roundToTwo(num) {
  var rounded = +(Math.round(num + "e+2") + "e-2");
  if (rounded < 0.01) {
    rounded = 0
  };
  return rounded
}


$("#quantity, #buy, #sell").on("change keyup paste", function() {
  var quantity = Number($('#quantity').val());
  var buy = Number($("#buy").val());
  var sell = Number($("#sell").val());


  var total_amnt_trade = roundToTwo((quantity * buy) + (quantity * sell));
  var brokerage_amnt_buy = ((buy * quantity) * 0.08) / 100;
  if (brokerage_amnt_buy >= 25) {
    var brokerage_buy = 25;
  } else {
    var brokerage_buy = brokerage_amnt_buy;
  }
  var brokerage_amnt_sell = ((sell * quantity) * 0.08) / 100;
  if (brokerage_amnt_sell >= 25) {
    var brokerage_sell = 25;
  } else {
    var brokerage_sell = brokerage_amnt_sell;
  }
  var brokerage = roundToTwo(brokerage_buy + brokerage_sell); //brokerage
  var transaction_charges = roundToTwo((((buy * quantity) + (sell * quantity)) * 0.00325) / 100); //Transaction Charges
  var gst = roundToTwo((((transaction_charges * 18) / 100) + (brokerage * 18) / 100)); //GST

  var total_charges = roundToTwo(brokerage + transaction_charges + gst);
  var pnl = roundToTwo(((sell - buy) * quantity) - total_charges);

  if (pnl == 0) {
    $('#pnl_text_display').text("Profit / Loss");
    $('#pnl_color').attr('bgcolor', '');
  } else if (pnl > 0) {
    $('#pnl_text_display').text("Profit");
    $('#pnl_color').attr('bgcolor', 'green');
  } else if (pnl < 0) {
    $('#pnl_text_display').text("Loss");
    $('#pnl_color').attr('bgcolor', 'red');
  }

  $('#tot_amnt_trade_display').text(total_amnt_trade);
  $('#total_charges_display').text(total_charges);
  $('#pnl_display').text(pnl);

  if (quantity > 0) {
    if ((buy > 0) || (sell > 0)) {
      var bep_pnl = -0.5;
      if (buy == 0) {
        buy = roundToTwo(sell - 0.01);
        while (bep_pnl < 0.01) {
          total_amnt_trade = roundToTwo((quantity * buy) + (quantity * sell));
          var brokerage_amnt_buy = ((buy * quantity) * 0.08) / 100;
          if (brokerage_amnt_buy >= 25) {
            var brokerage_buy = 25;
          } else {
            var brokerage_buy = brokerage_amnt_buy;
          }
          var brokerage_amnt_sell = ((sell * quantity) * 0.08) / 100;
          if (brokerage_amnt_sell >= 25) {
            var brokerage_sell = 25;
          } else {
            var brokerage_sell = brokerage_amnt_sell;
          }
          var brokerage = roundToTwo(brokerage_buy + brokerage_sell); //brokerage
          var transaction_charges = roundToTwo((((buy * quantity) + (sell * quantity)) * 0.00325) / 100); //Transaction Charges
          var gst = roundToTwo((((transaction_charges * 18) / 100) + (brokerage * 18) / 100)); //GST

          var total_charges = roundToTwo(brokerage + transaction_charges + gst);
          bep_pnl = roundToTwo(((sell - buy) * quantity) - total_charges);
          buy = roundToTwo(buy - 0.01);
        }
        var bep = roundToTwo(sell - buy);
        $('#bep_display').text(bep);
      } else {
        sell = roundToTwo(buy + 0.001);
        while (bep_pnl < 0.01) {
          total_amnt_trade = roundToTwo((quantity * buy) + (quantity * sell));
          var brokerage_amnt_buy = ((buy * quantity) * 0.08) / 100;
          if (brokerage_amnt_buy >= 25) {
            var brokerage_buy = 25;
          } else {
            var brokerage_buy = brokerage_amnt_buy;
          }
          var brokerage_amnt_sell = ((sell * quantity) * 0.08) / 100;
          if (brokerage_amnt_sell >= 25) {
            var brokerage_sell = 25;
          } else {
            var brokerage_sell = brokerage_amnt_sell;
          }
          var brokerage = roundToTwo(brokerage_buy + brokerage_sell); //brokerage
          var transaction_charges = roundToTwo((((buy * quantity) + (sell * quantity)) * 0.00325) / 100); //Transaction Charges
          var gst = roundToTwo((((transaction_charges * 18) / 100) + (brokerage * 18) / 100)); //GST

          var total_charges = roundToTwo(brokerage + transaction_charges + gst);
          bep_pnl = roundToTwo(((sell - buy) * quantity) - total_charges);
          sell = roundToTwo(sell + 0.01);
        }
        var bep = roundToTwo(sell - buy);
        $('#bep_display').text(bep);
      }
    }
  }

})
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>


<table class="table table-borderless">
  <tbody>
    <tr style="text-align: center;">
      <td><b>Quantity</b></td>
      <td><b>Buy</b></td>
      <td><b>Sell</b></td>
    </tr>
    <tr>
      <td><input type="number" class="form-control" id="quantity" placeholder="Quantity" name="quantity"></td>
      <td><input type="number" class="form-control" id="buy" placeholder="Buy Amount" name="buy"></td>
      <td><input type="number" class="form-control" id="sell" placeholder="Sell Amount" name="sell"></td>
    </tr>
  </tbody>
</table>
<table class="table table-dark right_side_col">
  <tbody>
    <tr>
      <td>Total Amount Traded</td>
      <td><span id="tot_amnt_trade_display"></span></td>
    </tr>
    <tr id="pnl_color">
      <td><span id="pnl_text_display">Profit / Loss</span></td>
      <td><span id="pnl_display"></span></td>
    </tr>
    <tr>
      <td>Breakeven Point</td>
      <td><span id="bep_display"></span></td>
    </tr>
  </tbody>
</table>
<table class="table table-dark right_side_col">
  <tbody>
    <tr>
      <td>Total Charges</td>
      <td><span id="total_charges_display"></span></td>
    </tr>
  </tbody>
</table>


此外,还请帮助我我的收支平衡点( BEP )公式正确与否,还有没有更简单的方法来编写逻辑代码?

  

BEP是指交易者在交易后既不亏损也不获利的情况。

1 个答案:

答案 0 :(得分:0)

您的代码存在的问题是,在函数roundToTwo中,您写道,如果数字小于0.01,则返回0。因此,它将永远不会返回小于0的数字。因此,只需删除该数字即可。 if statement,它将按预期运行。

function roundToTwo(num) {
  return +(Math.round(num + "e+2") + "e-2");
}