计算时从输入中删除NaN

时间:2017-11-14 09:36:48

标签: javascript

我有一个javascript函数,我需要在计算后从文本框中删除NaN

function calculateInterest() {

    var repay_period = parseInt(document.getElementById("emp_loan_repayment_method_id").value);
    var loan_amount = parseInt(document.getElementById("maximum_loan_amount").value);
    var rate_of_interest = parseInt(document.getElementById("rate_of_interest").value);
    var no_of_months = parseInt(document.getElementById("repayment_periods").value);
    var repay_month_amount = parseInt(document.getElementById("repayment_amount").value);
    var no_of_periods = no_of_months / 12;

    {
        var payable_interest = (loan_amount * no_of_periods * rate_of_interest) / 100;

        var pay_interest = payable_interest.toFixed(2);

        document.getElementById("total_payable_interest").value = pay_interest;


        var repay_amount = (loan_amount / no_of_months).toFixed(2);

        document.getElementById("repayment_amount").value = repay_amount;

        var total_payable_amount = (payable_interest + loan_amount).toFixed(2);

        document.getElementById("total_payable_amount").value = total_payable_amount;


    }
}

4 个答案:

答案 0 :(得分:1)

使用isNaN()功能

示例:

 var repay_amount = (loan_amount / no_of_months).toFixed(2);

 if(!isNaN(repay_amount)) //if is NaN don't set value
   document.getElementById("repayment_amount").value = repay_amount;

答案 1 :(得分:0)

为文本框分配值时,请检查它是否为NaN:

if(!isNaN(parseInt(value_to_test)))
    document.getElementById("yout_input").value = value_to_test;
else
    document.getElementById("yout_input").value = '0'; // or anything you want to show here

答案 2 :(得分:0)

您可以使用isNaN() javascript方法更改下面的代码

{
        var payable_interest = (loan_amount * no_of_periods * rate_of_interest) / 100;

        var pay_interest = payable_interest.toFixed(2);

        document.getElementById("total_payable_interest").value = isNaN(pay_interest) ? 0 : pay_interest;


        var repay_amount = (loan_amount / no_of_months).toFixed(2);

        document.getElementById("repayment_amount").value = isNaN(repay_amount) ? 0 : repay_amount;

        var total_payable_amount = (payable_interest + loan_amount).toFixed(2);

        document.getElementById("total_payable_amount").value = isNaN(total_payable_amount) ? 0 : total_payable_amount;


    }

答案 3 :(得分:0)

使用isNan()可以帮助您:

见下面的代码:

function calculateInterest() {

    var repay_period = parseInt(document.getElementById("emp_loan_repayment_method_id").value) ;
    var loan_amount = parseInt(document.getElementById("maximum_loan_amount").value);
    var rate_of_interest = parseInt(document.getElementById("rate_of_interest").value);
    var no_of_months = parseInt(document.getElementById("repayment_periods").value);
    var repay_month_amount = parseInt(document.getElementById("repayment_amount").value);
    var no_of_periods = no_of_months / 12;
    
    {
        var payable_interest = (loan_amount * no_of_periods * rate_of_interest) / 100;
    
        var pay_interest = payable_interest.toFixed(2);
        pay_interest = isNaN(pay_interest) ? 0 : pay_interest;
        document.getElementById("total_payable_interest").value = pay_interest;


        var repay_amount = (loan_amount / no_of_months).toFixed(2);
        repay_amount = isNaN(repay_amount) ? 0 : repay_amount;
        document.getElementById("repayment_amount").value = repay_amount;

        var total_payable_amount = (payable_interest + loan_amount).toFixed(2);
        total_payable_amount = isNaN(total_payable_amount) ? 0 : total_payable_amount;
        document.getElementById("total_payable_amount").value = total_payable_amount;


    }
}
emp_loan_repayment_method : <input id="emp_loan_repayment_method_id" value="100" />
<br>
maximum_loan_amount : <input id="maximum_loan_amount" value ="50" />
<br>
rate_of_interest : <input id="rate_of_interest" value="2" />
<br>
repayment_periods : <input id="repayment_periods" value="3" />
<br>
repayment_amount : <input id="repayment_amount" value="200"  />

<br><br><br>

total_payable_interest : <input id="total_payable_interest" />
<br><br>
total_payable_amount : <input id="total_payable_amount" />
<br><br>

<button onclick="calculateInterest()" >Calculate</button>