-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.vue
131 lines (119 loc) · 3.53 KB
/
App.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<template>
<div class="container">
<div class="row">
<label class="label">Principal : </label>
<input class="input" type="text" placeholder="Loan amount plus fee(if any)" v-model="loanAmount">
<label class="label">Interest rate : </label>
<input class="input" type="text" placeholder="% per payment" v-model="interestRate">
<label class="label">Number Of Payments(Months)</label>
<input class="input" type="text" placeholder="Number of payments" v-model="paymentNumber">
</div>
<button class="button" @click="calc_payments" >
Calculate
</button>
<div v-if="payments.length">
<p>Monthly principal & interest: {{ monthlyPayment.toFixed(2) }}</p>
<p>Total Interest: {{ totalInterest.toFixed(2) }}</p>
<p>Total of all monthly payments: {{ totalPayment.toFixed(2) }}</p>
</div>
<table class="table" cellspacing="10" v-if="payments.length">
<thead>
<tr>
<th>Number</th>
<th>Payment</th>
<th>Interest</th>
<th>Principal</th>
<th>Balance</th>
</tr>
</thead>
<tbody>
<tr v-for="payment in payments" :key="payment.count">
<td>{{ payment.count }}</td>
<td>{{ payment.payment.toFixed(2) }}</td>
<td>{{ payment.interest.toFixed(2) }}</td>
<td>{{ payment.principal.toFixed(2) }}</td>
<td>{{ payment.balance.toFixed(2) }}</td>
</tr>
</tbody>
</table>
</div>
</template>
<script type="text/javascript">
export default {
mounted(){
},
data(){
return {
loanAmount: 300000,
paymentNumber:360,
interestRate:4.625,
payments:[],
}
},
methods:{
reset(){
this.loanAmount = '';
this.paymentNumber = '';
this.interestRate = '';
this.monthlyPayment = '';
this.payments=[];
},
calc_payment(p = this.loanAmount, n = this.paymentNumber, int = this.interestRate){
// https://www.vertex42.com/ExcelArticles/amortization-calculation.html
// r(1 + r)^n
// pmt = p * --------------
// (1 + r)^n - 1
let r = int / 100 / 12
let pmt = p * (r * Math.pow((1 + r), n) / (Math.pow((1 + r), n) - 1));
this.monthlyPayment = pmt
return pmt
},
calc_payments(e, balance = parseFloat(this.loanAmount), rate = this.interestRate){
var count = 0;
var payments = [];
var totalPayment = 0;
var totalInterest = 0;
let payment = Number(this.calc_payment())
do {
count++
var interest = (balance * (rate/12/100))
var principal = (payment - interest)
if (balance < payment) {
principal = balance;
payment = (Number(interest) + Number(principal))
}
balance = balance - principal
if (balance < 0) {
principal = principal + balance;
interest = Number(interest) - balance;
balance = 0;
}
payments.push({
balance: balance, payment: payment,
interest: interest,
principal: principal,
count
});
totalPayment = totalPayment + payment;
totalInterest = totalInterest + interest;
} while (balance > 0);
this.payments = payments;
this.totalInterest = totalInterest;
this.totalPayment = totalPayment;
}
}
}
</script>
<style type="text/css" scoped>
table{
margin-top: 5%;
border-top: 1px solid #ccc;
}
input {
max-width: 100px;
}
.row {
display: flex;
flex-direction: column;
}
</style>