-
-
Save ghalimi/4583240 to your computer and use it in GitHub Desktop.
// Copyright (c) 2012 Sutoiku, Inc. (MIT License) | |
// Some algorithms have been ported from Apache OpenOffice: | |
/************************************************************** | |
* | |
* Licensed to the Apache Software Foundation (ASF) under one | |
* or more contributor license agreements. See the NOTICE file | |
* distributed with this work for additional information | |
* regarding copyright ownership. The ASF licenses this file | |
* to you under the Apache License, Version 2.0 (the | |
* "License"); you may not use this file except in compliance | |
* with the License. You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, | |
* software distributed under the License is distributed on an | |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
* KIND, either express or implied. See the License for the | |
* specific language governing permissions and limitations | |
* under the License. | |
* | |
*************************************************************/ | |
function CUMIPMT(rate, periods, value, start, end, type) { | |
// Credits: algorithm inspired by Apache OpenOffice | |
// Credits: Hannes Stiebitzhofer for the translations of function and variable names | |
// Requires getFutureValue() and getPartialPayment() from Formula.js [http://stoic.com/formula/] | |
// Evaluate rate and periods (TODO: replace with secure expression evaluator) | |
rate = eval(rate); | |
periods = eval(periods); | |
// Return error if either rate, periods, or value are lower than or equal to zero | |
if (rate <= 0 || periods <= 0 || value <= 0) return '#NUM!'; | |
// Return error if start < 1, end < 1, or start > end | |
if (start < 1 || end < 1 || start > end) return '#NUM!'; | |
// Return error if type is neither 0 nor 1 | |
if (type !== 0 && type !== 1) return '#NUM!'; | |
// Compute cumulative interest | |
var payment = getPartialPayment(rate, periods, value, 0, type); | |
var interest = 0; | |
interest = 0; | |
if(start === 1) { | |
if(type === 0) { | |
interest = -value; | |
start++; | |
} | |
} | |
for (var i = start; i <= end; i++) { | |
if (type === 1) { | |
interest += getFutureValue(rate, i - 2, payment, value, 1 ) - payment; | |
} else { | |
interest += getFutureValue(rate, i - 1, payment, value, 0 ); | |
} | |
} | |
interest *= rate; | |
// Return cumulative interest | |
return interest; | |
} |
Here is CUMIPMT with the getFutureValue and getPartialPayment functions.
Copied this straight from here
exports.CUMIPMT = function(rate, periods, value, start, end, type) {
// Credits: algorithm inspired by Apache OpenOffice
// Credits: Hannes Stiebitzhofer for the translations of function and variable names
// Requires exports.FV() and exports.PMT() from exports.js [http://stoic.com/exports/]
rate = utils.parseNumber(rate);
periods = utils.parseNumber(periods);
value = utils.parseNumber(value);
if (utils.anyIsError(rate, periods, value)) {
return error.value;
}
// Return error if either rate, periods, or value are lower than or equal to zero
if (rate <= 0 || periods <= 0 || value <= 0) {
return error.num;
}
// Return error if start < 1, end < 1, or start > end
if (start < 1 || end < 1 || start > end) {
return error.num;
}
// Return error if type is neither 0 nor 1
if (type !== 0 && type !== 1) {
return error.num;
}
// Compute cumulative interest
var payment = exports.PMT(rate, periods, value, 0, type);
var interest = 0;
if (start === 1) {
if (type === 0) {
interest = -value;
start++;
}
}
for (var i = start; i <= end; i++) {
if (type === 1) {
interest += exports.FV(rate, i - 2, payment, value, 1) - payment;
} else {
interest += exports.FV(rate, i - 1, payment, value, 0);
}
}
interest *= rate;
// Return cumulative interest
return interest;
};
exports.FV = function(rate, periods, payment, value, type) {
// Credits: algorithm inspired by Apache OpenOffice
value = value || 0;
type = type || 0;
rate = utils.parseNumber(rate);
periods = utils.parseNumber(periods);
payment = utils.parseNumber(payment);
value = utils.parseNumber(value);
type = utils.parseNumber(type);
if (utils.anyIsError(rate, periods, payment, value, type)) {
return error.value;
}
// Return future value
var result;
if (rate === 0) {
result = value + payment * periods;
} else {
var term = Math.pow(1 + rate, periods);
if (type === 1) {
result = value * term + payment * (1 + rate) * (term - 1) / rate;
} else {
result = value * term + payment * (term - 1) / rate;
}
}
return -result;
};
exports.PMT = function(rate, periods, present, future, type) {
// Credits: algorithm inspired by Apache OpenOffice
future = future || 0;
type = type || 0;
rate = utils.parseNumber(rate);
periods = utils.parseNumber(periods);
present = utils.parseNumber(present);
future = utils.parseNumber(future);
type = utils.parseNumber(type);
if (utils.anyIsError(rate, periods, present, future, type)) {
return error.value;
}
// Return payment
var result;
if (rate === 0) {
result = (present + future) / periods;
} else {
var term = Math.pow(1 + rate, periods);
if (type === 1) {
result = (future * rate / (term - 1) + present * rate / (1 - 1 / term)) / (1 + rate);
} else {
result = future * rate / (term - 1) + present * rate / (1 - 1 / term);
}
}
return -result;
};
Hi,
How do i use the CUMIPMT function?
I'm trying to use it on blogger and have inserted the following code in a HTML post , but nothing comes out. Im trying to reference an external JS file using the steps shown in http://helplogger.blogspot.sg/2014/09/host-blogger-css-javascript-files-in-google-drive.html but it doesnt seem to work.
<script src="https://cdnjs.cloudflare.com/ajax/libs/numeric/1.2.6/numeric.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/numeral.js/1.4.5/numeral.min.js"></script> <script src="https://cdn.jsdelivr.net/jstat/latest/jstat.min.js"></script>//uploaded the formula.js file into google drive together with its related other folders according to https://github.com/sutoiku/formula.js
<script src="https://drive.google.com/file/d/0B74BAo6Pq1tyOHVuaHNVc1NzRVk"></script>//uploaded the CUMIPMT.js file into google drive
<script src="https://drive.google.com/file/d/0B74BAo6Pq1tyaDBqdVM0eENpb1E"></script> <script> document.write(CUMIPMT(12/12, 10, 10000, 1, 10, 0)); </script>The CUMIPMT calculation mentioned here is wrong. I compared the calculation value with Google Sheet or Excel and it is showing smaller value than CUMIPMT value shown in Google Sheet. Can someone tell me why?
I dont find in Formula.js funcrions getFutureValue() and getPartialPayment()