Skip to content

Instantly share code, notes, and snippets.

@iwamoto-takaaki
Created March 7, 2018 00:50
Show Gist options
  • Save iwamoto-takaaki/aa2da6877245cd86970e0c065d11c329 to your computer and use it in GitHub Desktop.
Save iwamoto-takaaki/aa2da6877245cd86970e0c065d11c329 to your computer and use it in GitHub Desktop.
loancalc
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript">
function calcPayment(loan, termRate, numberOfTerms, loanType, count){
if (count < 1) return 0;
if (loanType == "payment_equal") {
return calcPaymentEqual(loan, termRate, numberOfTerms) * count;
}else{
return calcPrincipalEqual(loan, termRate, numberOfTerms)
+ calcPayment(loan * (numberOfTerms - 1) / numberOfTerms, termRate, numberOfTerms - 1, loanType, count - 1);
}
}
function calcPaymentEqual(loan, termRate, numberOfTerms){
return Math.floor(
loan * termRate * Math.pow(1 + termRate, numberOfTerms)
/ (Math.pow(1 + termRate, numberOfTerms) - 1)
);
}
function calcPrincipalEqual(loan, termRate, numberOfTerms){
return Math.floor(
(loan / numberOfTerms) + loan * termRate
);
}
function calc (){
function isInteger(val) {
return $.isNumeric(val) && (val % 1 == 0);
}
$("#error_message").html("");
$("#monthly_payment").html("");
$("#bounus_payment").html("");
$("#annual_payment").html("");
$("#total_payment").html("");
if (isInteger($("input[name=loan]").val()) == false) {
$("#error_message").html("借入額は整数を入力してください。");
}
if ($("input[name=loan]").val() < 1) {
$("#error_message").html("借入額は1万円以上を入力してください。");
}
if ($("input[name=bounus_payment]").val() == "") $("input[name=bounus_payment]").val(0);
if (isInteger($("input[name=bounus_payment]").val()) == false) {
$("#error_message").html("ボーナス支払い額は整数を入力してください。");
}
if ($("input[name=bounus_payment]").val() < 0) {
$("#error_message").html("ボーナス支払い額は0円以上を入力してください。");
}
if (isInteger($("input[name=priod]").val()) == false) {
$("#error_message").html("支払い年数は整数を入力してください。");
}
if ($("input[name=priod]").val() <= 1 ) {
$("#error_message").html("支払い年数は1年以上を入力してください。");
}
if ($.isNumeric($("input[name=rate]").val()) == false) {
$("#error_message").html("利率は数値を入力してください。");
}
if ($("input[name=rate]").val() <= 0 || 15 <= $("input[name=rate]").val()) {
$("#error_message").html("利率額は0%より大きく~15%より小さい値を入力してください。");
}
loan = parseInt($("input[name=loan]").val()) * 10000;
bounusloan = parseInt($("input[name=bounus_payment]").val()) * 2 * parseInt($("input[name=priod]").val()) * 10000;
rate = parseFloat($("input[name=rate]").val()) / 100;
priod = parseInt($("input[name=priod]").val());
type = $("input[name=loan_type]:checked").val();
if(loan < bounusloan){
$("#error_message").html("ボーナス支払い額が借入に対して大きすぎます。");
}
if ($("#error_message").html() != "") return;
$("#monthly_payment").html(calcPayment(loan - bounusloan, rate / 12, priod * 12, type, 1));
$("#bounus_payment").html(calcPayment(bounusloan, rate / 2, priod * 2, type, 1));
$("#annual_payment").html(
calcPayment(loan - bounusloan, rate / 12, priod * 12, type, 12)
+ calcPayment(bounusloan, rate / 2, priod * 2, type, 2));
$("#total_payment").html(
calcPayment(loan - bounusloan, rate / 12, priod * 12, type, priod * 12)
+ calcPayment(bounusloan, rate / 2, priod * 2, type, priod * 2));
}
</script>
<div id="loan_simulater">
<div>借入額
<input type="text" name="loan">
万円
</div>
<div>ボーナス支払い額(年2回)
<input type="text" name="bounus_payment">
万円
</div>
<div>支払い年数
<input type="text" name="priod">
</div>
<div>利率
<input type="text" name="rate">
</div>
<div>元利均等返済
<input type="radio" name="loan_type" value="payment_equal" checked="">
</div>
<div>元本均等返済
<input type="radio" name="loan_type" value="principal_equal">
</div>
<div id="error_message" style="color:red" ></div>
<div>
<input type="button" name="calc" onclick="calc()" value="計算">
</div>
<div>返済額
<div id="monthly_payment" >0</div>
円(元本一括払いのとき1回目の支払い金額)
</div>
<div>ボーナス払い
<div id="bounus_payment" >0</div>
円(元本一括払いのとき1回目の支払い金額)
</div>
<div>年間返済額
<div id="annual_payment" >0</div>
</div>
<div>総返済額
<div id="total_payment" >0</div>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment