LoginSignup
0
0

More than 3 years have passed since last update.

javascriptで複利計算プログラム

Posted at

 <script type="text/javascript">
      function Calculator() {
        var rate, amount, period;
        rate = parseInt(document.myform.rate.value, 10);
        amount = parseInt(document.myform.amount.value, 10);
        period = parseInt(document.myform.period.value, 10);
        document.myform.receipt.value = fukuri(rate, amount, period);
      }

      function fukuri(rate, amount, period) {
        var receipt;
        receipt = amount;

        var str = "";
        str += "<table class=’test’>";
        str += "<tr><th>預入期間</th><th>受取金額</th></tr>";

        //預入金額=元金×(1+金利)/100
        //Math.round:小数点以下を四捨五入
        for (var i = 1; i <= period; i++) {
          receipt = Math.round(receipt * (1 + rate / 100));
          str += "<tr><td>" + i + "年</td><td>" + receipt + "円</td></tr>";
        }

        str += "</table>";
        document.getElementById("result").innerHTML = str;
        return receipt;
      }

      function TableClear() {
        document.getElementById("result").innerHTML = "";
      }
    </script>

  <body>
    <h1>複利計算</h1>
    <p>金利・金額・預入期間(年)を入力してください。</p>
    <form name="myform">
      <table>
        <tr>
          <th>金利</th>
          <td><input type="text" name="rate" size="22" value="4" /></td>
        </tr>
        <tr>
          <th>金額</th>
          <td><input type="text" name="amount" size="22" value="10000" /></td>
        </tr>
        <tr>
          <th>預入期間</th>
          <td><input type="text" name="period" size="22" value="10" /></td>
        </tr>
        <tr>
          <td colspan="2" class="btn">
            <input type="button" onclick="Calculator()" value="計算" />
            <input type="reset" onclick="TableClear()" value="クリア" />
          </td>
        </tr>
        <tr>
          <th>受取金額</th>
          <td><input type="text" name="receipt" size="22" /></td>
        </tr>
      </table>
    </form>
    <hr />
    <div id="result"></div>
  </body>

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0