javascript贷款等额本金,等额本息计算示例

   javascript贷款等额本金,等额本息计算示例

<title>javascript贷款等额本金,等额本息计算示例</title>
<form>
    <div class="input">
        <table cellpadding="0" cellspacing="1">
            <tbody>
                <tr>
                    <td>贷款额度利率:</td>
                    <td>
                        <input id="daikuan" type="text" class="cc7" />元
                    </td>
                </tr>
                <tr>
                    <td align="right">
                        年利率:
                    </td>
                    <td>
                        <input id="ll" type="text" class="cc7" />%
                    </td>
                </tr>

                <tr>
                    <td align="right">期数  </td>
                    <td>
                        <select id="qishu">
                            <option value="">请选择</option>
                            <script>
                                for (var i = 1; i <= 30; i++) document.write('<option value="' + i + '"> ' + i + '年</option>');
                            </script>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td align="right">还款方式: </td>
                    <td>
                        <input type="radio" name="ben" id="benjin">本金
                        <input type="radio" name="ben" id="benxi" style="margin-left:20px;">本息
                    </td>

                </tr>
            </tbody>

        </table>
        <input type="button" value="开始计算" onclick="return calculate(this.form)" />
        <input type="reset" value="重置" />

    </div>
    <div class="output">

        <h4>计算结果</h4>
        <hr class="hi" />
        <div>
            <table id="table" border="1" cellspacing="0" cellpadding="10" width="100%">
                <thead>
                    <tr>
                        <td>期次</td>
                        <td>未偿还本金</td>
                        <td>偿还本金</td>
                        <td>偿还利息</td>
                        <td>偿还本息和</td>

                    </tr>
                </thead>
                <tbody id="list"></tbody>
            </table>
        </div>

    </div>
</form> 
<script>
    function calculate(f) {
        var rx = /^\d+(\.\d+)?$/;
        if (!rx.test(f.daikuan.value)) { alert('贷款额度为数字!'); f.daikuan.select(); return false }
        if (!rx.test(f.ll.value)) { alert('利率为数字!'); f.ll.select(); return false }
        if (f.qishu.value == '') { alert('请选择期数!'); f.qishu.focus(); return false }
        if (!f.ben[0].checked && !f.ben[1].checked) { alert('请选择还款方式!'); f.ben[0].focus(); return false }
        var jine = parseFloat(f.daikuan.value), ll = parseFloat(f.ll.value), qishu = parseFloat(f.qishu.value)*12, benjin = f.ben[0].checked;


        ll /= 100 * 12;//转为月利率

        var list = document.getElementById('list'), tr, total = jine;
        while (list.childNodes.length > 0) list.removeChild(list.firstChild);

        //等额本息月还款额=本金*月利率*(1+月利率)^n/[(1+月利率)^n-1] 
        var money = benjin ? jine / qishu : jine * ll * Math.pow(1 + ll, qishu) / (Math.pow(1 + ll, qishu) - 1),
            totalLX = 0, lx;
        for (var i = 1; i <= qishu; i++) {
            tr = list.insertRow(i - 1); tr.setAttribute('align', 'right');
            td = tr.insertCell(0); td.innerHTML = i;
            td = tr.insertCell(1); td.innerHTML = jine.toFixed(2);
            lx = jine * ll;
            totalLX += lx;
            td = tr.insertCell(2); td.innerHTML = benjin ? money.toFixed(2) : (money - lx).toFixed(2);
            td = tr.insertCell(3); td.innerHTML = lx.toFixed(2);
            td = tr.insertCell(4); td.innerHTML = benjin ? (lx + money).toFixed(2) : money.toFixed(2);

            jine -= money - (benjin ? 0 : lx);
        }
        tr = list.insertRow(qishu); tr.setAttribute('align', 'right');

        td = tr.insertCell(0); td.innerHTML = '合计';
        td = tr.insertCell(1); td.innerHTML = jine.toFixed(2);
        td = tr.insertCell(2); td.innerHTML = (total - jine).toFixed(2);
        td = tr.insertCell(3); td.innerHTML = totalLX.toFixed(2);
        td = tr.insertCell(4); td.innerHTML = (total - jine + totalLX).toFixed(2);;
        return false
    }
</script>

 


原创文章,转载请注明出处:javascript贷款等额本金,等额本息计算示例

评论(0)Web开发网
阅读(142)喜欢(0)不喜欢(0)JavaScript/Ajax开发技巧