[JavaScript] 0.05% 거래 수수료 소스

거래 수수료 소스

자바스크립트를 이용하여 0.05% 거래 수수료가 있을 때, 손익을 계산하는 간단한 계산기 소스입니다.

<head>
  <style>
    #btnCalcBeomSang {
      font-size: 16px;
      height: 32px;
      padding: 6px 12px;
      color: #ffffff;
      background-color: #4CAF50;
      border: none;
      border-radius: 4px;
      cursor: pointer;
    }
  </style>
  <script>
    function getUnitPrice(price) {
      let unitPrice;
      if (price >= 1000000) {
        unitPrice = 1000;
      } else if (price >= 500000) {
        unitPrice = 100;
      } else if (price >= 100000) {
        unitPrice = 50;
      } else if (price >= 10000) {
        unitPrice = 10;
      } else if (price >= 1000) {
        unitPrice = 5;
      } else if (price >= 100) {
        unitPrice = 1;
      } else if (price >= 10) {
        unitPrice = 0.1;
      } else if (price >= 1) {
        unitPrice = 0.01;
      } else {
        unitPrice = 0.001;
      }
      return unitPrice;
    }

    function calculate_profit() {
      const buyFee = 0.05 / 100;
      const selFee = 0.05 / 100;
      let buyAmt = parseFloat(document.getElementById("buyBeomSang").value);
      let selAmt = parseFloat(document.getElementById("sellBeomSang").value);
      let proLosAmt;
      let amtToSell;
      let unitPrice;
      let tmpAmt;

      document.getElementById("resultBeomSang").style.color = 'black';
      document.getElementById("resultBeomSang").innerHTML = '';
      document.getElementById("result2BeomSang").innerHTML = '';

      if (!buyAmt) {
        document.getElementById("resultBeomSang").innerHTML = "구매금액이 없습니다. 값을 입력해주세요.";
        return;
      }

      if (selAmt) {
        proLosAmt = selAmt * (1 - selFee) - buyAmt * (1 + buyFee);
      }

      amtToSell = buyAmt;
      while (true) {
        amtToSell += getUnitPrice(amtToSell);
        if (amtToSell * (1 - selFee) - buyAmt * (1 + buyFee) > 0) {
          break;
        }
      }

      document.getElementById("resultBeomSang").innerHTML = "손익: " + (proLosAmt ? proLosAmt.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',') : "미정");
      document.getElementById("result2BeomSang").innerHTML = "손익분기액 : " + amtToSell.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
      if (proLosAmt && proLosAmt < 0) {
        document.getElementById("resultBeomSang").style.color = 'red';
      }
    }
  </script>
</head>

<body>
  <div class="form-container">
    <label for="buyBeomSang">구매금액(₩): </label>
    <input type="number" id="buyBeomSang" placeholder="예) 1,000,000">
    <br>
    <label for="sellBeomSang">판매금액(₩): </label>
    <input type="number" id="sellBeomSang">
    <br>
    <button id="btnCalcBeomSang" onclick="calculate_profit()">계산하기</button>
  </div>
  <div class="result" id="resultBeomSang"></div>
  <div class="result" id="result2BeomSang"></div>
</body>

--추가합니다.

<div class="form-container">
    <label for="buyBeomSang">구매금액(₩): </label>
    <input type="number" id="buyBeomSang" placeholder="예) 1,000,000">
    <br>
    <label for="sellBeomSang">판매금액(₩): </label>
    <input type="number" id="sellBeomSang">
    <br>
    <button id="btnCalcBeomSang" onclick="calculate_profit()">계산하기</button>
    <div class="result" id="resultBeomSang" style="color: black;">구매금액이 없습니다. 값을 입력해주세요.</div>
    <div class="result" id="result2BeomSang"></div>
  </div>
<script>
    function getUnitPrice(price) {
      let unitPrice;
      if (price >= 1000000) {
        unitPrice = 1000;
      } else if (price >= 500000) {
        unitPrice = 100;
      } else if (price >= 100000) {
        unitPrice = 50;
      } else if (price >= 10000) {
        unitPrice = 10;
      } else if (price >= 1000) {
        unitPrice = 5;
      } else if (price >= 100) {
        unitPrice = 1;
      } else if (price >= 10) {
        unitPrice = 0.1;
      } else if (price >= 1) {
        unitPrice = 0.01;
      } else {
        unitPrice = 0.001;
      }
      return unitPrice;
    }

    function calculate_profit() {
      const buyFee = 0.05 / 100;
      const selFee = 0.05 / 100;
      let buyAmt = parseFloat(document.getElementById("buyBeomSang").value);
      let selAmt = parseFloat(document.getElementById("sellBeomSang").value);
      let proLosAmt;
      let amtToSell;
      let unitPrice;
      let tmpAmt;

      document.getElementById("resultBeomSang").style.color = 'black';
      document.getElementById("resultBeomSang").innerHTML = '';
      document.getElementById("result2BeomSang").innerHTML = '';

      if (!buyAmt) {
        document.getElementById("resultBeomSang").innerHTML = "구매금액이 없습니다. 값을 입력해주세요.";
        return;
      }

      if (selAmt) {
        proLosAmt = selAmt * (1 - selFee) - buyAmt * (1 + buyFee);
      }

      amtToSell = buyAmt;
      while (true) {
        amtToSell += getUnitPrice(amtToSell);
        if (amtToSell * (1 - selFee) - buyAmt * (1 + buyFee) > 0) {
          break;
        }
      }

      document.getElementById("resultBeomSang").innerHTML = "손익: " + (proLosAmt ? proLosAmt.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',') : "미정");
      document.getElementById("result2BeomSang").innerHTML = "손익분기액 : " + amtToSell.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
      if (proLosAmt && proLosAmt < 0) {
          document.getElementById("resultBeomSang").style.color = 'red';        
      }
    }
  </script>
<style>
    #btnCalcBeomSang {
      font-size: 16px;
      height: 32px;
      padding: 6px 12px;
      color: #ffffff;
      background-color: #4CAF50;
      border: none;
      border-radius: 4px;
      cursor: pointer;
    }
  </style>

댓글