-
Notifications
You must be signed in to change notification settings - Fork 22
/
finance.js
281 lines (230 loc) · 7.31 KB
/
finance.js
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* finance.js v0.1
* By: Trent Richardson [http://trentrichardson.com]
*
* Copyright 2012 Trent Richardson
* You may use this project under MIT or GPL licenses.
* http://trentrichardson.com/Impromptu/GPL-LICENSE.txt
* http://trentrichardson.com/Impromptu/MIT-LICENSE.txt
*/
;(function(root){
var lib = {};
lib.version = '0.1';
/*
* Defaults
*/
lib.settings = {
format: 'number',
formats: {
USD: { before: '$', after: '', precision: 2, decimal: '.', thousand: ',', group: 3, negative: '-' }, // $
GBP: { before:'£', after: '', precision: 2, decimal: '.', thousand: ',', group: 3, negative: '-' }, // £ or £
EUR: { before:'€', after: '', precision: 2, decimal: '.', thousand: ',', group: 3, negative: '-' }, // € or €
percent: { before: '', after: '%', precision: 0, decimal: '.', thousand: ',', group: 3, negative: '-' },
number: { before: '', after: '', precision: null, decimal: '.', thousand: ',', group: 3, negative: '-'},
defaults: { before: '', after: '', precision: 0, decimal: '.', thousand: ',', group: 3, negative: '-' }
}
};
lib.defaults = function(object, defs) {
var key;
object = object || {};
defs = defs || {};
for (key in defs) {
if (defs.hasOwnProperty(key)) {
if (object[key] == null) object[key] = defs[key];
}
}
return object;
};
/*
* Formatting
*/
// add a currency format to library
lib.addFormat = function(key, options){
this.settings.formats[key] = this.defaults(options, this.settings.formats.defaults);
return true;
};
// remove a currency format from library
lib.removeFormat = function(key){
delete this.settings.formats[key];
return true;
};
// format a number or currency
lib.format = function(num, settings, override){
num = parseFloat(num);
if(settings === undefined)
settings = this.settings.formats[this.settings.format];
else if(typeof settings == 'string')
settings = this.settings.formats[settings];
else settings = settings;
settings = this.defaults(settings, this.settings.formats.defaults);
if(override !== undefined)
settings = this.defaults(override, settings);
// set precision
if(settings.precision != null)
num = num.toFixed(settings.precision);
var isNeg = num < 0,
numParts = Math.abs(num).toString().split('.'),
baseLen = numParts[0].length;
// add thousands and group
numParts[0] = numParts[0].replace(/(\d)/g, function(str, m1, offset, s){
return (offset > 0 && (baseLen-offset) % settings.group == 0)? settings.thousand + m1 : m1;
});
// add decimal
num = numParts.join(settings.decimal);
// add negative if applicable
if(isNeg && settings.negative){
num = settings.negative[0] + num;
if(settings.negative.length > 1)
num += settings.negative[1];
}
return settings.before + num + settings.after;
};
/*
* Financing
*/
// calculate total of principle + interest (yearly) for x months
lib.calculateAccruedInterest = function(principle, months, rate){
var i = rate/1200;
return (principle * Math.pow(1+i,months)) - principle;
};
// determine the amount financed
lib.calculateAmount = function(finMonths, finInterest, finPayment){
var result = 0;
if(finInterest == 0){
result = finPayment * finMonths;
}
else{
var i = ((finInterest/100) / 12),
i_to_m = Math.pow((i + 1), finMonths),
a = finPayment / ((i * i_to_m) / (i_to_m - 1));
result = Math.round(a * 100) / 100;
}
return result;
};
// determine the months financed
lib.calculateMonths = function(finAmount, finInterest, finPayment){
var result = 0;
if(finInterest == 0){
result = Math.ceil(finAmount / finPayment);
}
else{
result = Math.round(( (-1/12) * (Math.log(1-(finAmount/finPayment)*((finInterest/100)/12))) / Math.log(1+((finInterest/100)/12)) )*12);
}
return result;
};
// determine the interest rate financed http://www.hughchou.org/calc/formula.html
lib.calculateInterest = function(finAmount, finMonths, finPayment){
var result = 0;
var min_rate = 0, max_rate = 100;
while(min_rate < max_rate-0.0001){
var mid_rate = (min_rate + max_rate)/2,
j = mid_rate / 1200,
guessed_pmt = finAmount * ( j / (1-Math.pow(1+j, finMonths*-1)));
if(guessed_pmt > finPayment){
max_rate = mid_rate;
}
else{
min_rate = mid_rate;
}
}
return mid_rate.toFixed(2);
};
// determine the payment
lib.calculatePayment = function(finAmount, finMonths, finInterest){
var result = 0;
if(finInterest == 0){
result = finAmount / finMonths;
}
else{
var i = ((finInterest/100) / 12),
i_to_m = Math.pow((i + 1), finMonths),
p = finAmount * ((i * i_to_m) / (i_to_m - 1));
result = Math.round(p * 100) / 100;
}
return result;
};
// calculate time and money savings by paying extra
lib.calculateEarlyPayoff = function(finAmount, finInterest, finMonths, finRemainingMonths, finExtraPay, finMonthlyPay){
var principle1 = finAmount, interest1 = 0, principle2 = finAmount, interest2 = 0;
var ep;
var mRate = finInterest / 1200;
var paidOff = finMonths;
for (months=1; months<=finMonths; months++)
{
if ( months > (finMonths-finRemainingMonths)) {
ep = finExtraPay;
}
else {
ep = 0;
}
var mi1 = mRate * principle1;
mi1 = Math.round(mi1 * 100) / 100;
interest1 += mi1;
principle1 -= ( finMonthlyPay - mi1 );
if ( principle2 > 0 )
{
var mi2 = mRate * principle2;
mi2 = Math.round(mi2 * 100) / 100;
interest2 += mi2;
principle2 -= ( finMonthlyPay - mi2 + ep );
principle2 = Math.round(principle2 * 100) / 100;
if ( principle2 <= 0 ) {
principle2 = 0;
paidOff = months;
}
}
}
var timeDifference = finMonths - paidOff;
var y = parseInt( timeDifference/12, 10 );
months = timeDifference%12;
return {
saving: Math.round((interest1 - interest2) * 100) / 100,
years: y,
months: months
};
};
// get an amortization schedule [ { principle: 0, interest: 0, payment: 0, paymentToPrinciple: 0, paymentToInterest: 0}, {}, {}...]
lib.calculateAmortization = function(finAmount, finMonths, finInterest, finDate){
var payment = this.calculatePayment(finAmount, finMonths, finInterest),
balance = finAmount,
interest = 0.0,
totalInterest = 0.0,
schedule = [],
currInterest = null,
currPrinciple = null,
currDate = (finDate !== undefined && finDate.constructor === Date)? new Date(finDate) : (new Date());
for(var i=0; i<finMonths; i++){
currInterest = balance * finInterest/1200;
totalInterest += currInterest;
currPrinciple = payment - currInterest;
balance -= currPrinciple;
schedule.push({
principle: balance,
interest: totalInterest,
payment: payment,
paymentToPrinciple: currPrinciple,
paymentToInterest: currInterest,
date: new Date(currDate.getTime())
});
currDate.setMonth(currDate.getMonth()+1);
}
return schedule;
};
/*
* Export this object globally
*/
if(typeof exports !== 'undefined'){
if(typeof module !== 'undefined' && module.exports){
exports = module.exports = lib;
}
exports.finance = lib;
}
else if(typeof define === 'function' && define.amd){
define([], function(){
return lib;
});
}
else{
root.finance = lib;
}
})(this);