当我添加两个字段时,代码不会对其进行求和,而是添加我输入的内容,这是我的代码
function calculate_loan() {
var amountBorrowed = document.form.amountBorrowed.value;
var interest = document.form.interest.value / 100;
var payments = document.form.durationOfPayment.value;
var monthInterest = document.form.interestPerMonth.value;
var totalInt = document.form.totalinterest.value;
var monthly = amountBorrowed * interest;
var totalinterest = monthly * payments;
var totalAmount = amountBorrowed + totalinterest;
if (!isNaN(monthly) &&
(monthly != Number.POSITIVE_INFINITY) &&
(monthly != Number.NEGATIVE_INFINITY)) {
document.form.total.value = round(totalAmount);
document.form.totalinterest.value =
round(monthly * payments);
document.form.interestPerMonth.value =
round(monthly);
}
else {
document.form.amountOfPayement.value = "";
document.form.total.value = "";
document.form.totalinterest.value = "";
}
}
function round(x) {
return Math.round(x * 100) / 100;
}
例如每月利息是:720 总利息:2160 借款金额:12000 结果是 120002160。我希望结果是 14,160 请帮忙谢谢
您应该将值解析为数字
var amountBorrowed = Number(document.form.amountBorrowed.value);
var interest = Number(document.form.interest.value) / 100;
var payments = Number(document.form.durationOfPayment.value);
var monthInterest = Number(document.form.interestPerMonth.value);
var totalInt = Number(document.form.totalinterest.value);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)