JSFiddle - React, Tailwind, and code Playground
HTML
<div>Rate
<input type="text" class="rate" value="0.128/12"/>Per
<input type="text" class="per" value="63"/>NPer
<input type="text" class="nper" value="0"/>pmt
<input type="text" class="pmt" value="-3872917.00" />fv
<input type="text" class="fv" />
</div>
<button class="calcPV">Calculate PV</button>
<button class="calcFV">Calculate FV</button>
<br/>
<input type="text" class="total" placeholder="Total" />
CSS
div input {
display:block;
}
JavaScript
jQuery(document).ready(function () {
jQuery('.calcPV').click(function () {
var rate = Number(jQuery('.rate').val());
var per = Number(jQuery('.per').val());
var NPer = Number(jQuery('.NPer').val());
var pmt = Number(jQuery('.pmt').val());
var fv = Number(jQuery('.fv').val());
var pvTot = pv(rate, per, NPer, pmt, fv);
jQuery('.total').val(pvTot);
});
jQuery('.calcFV').click(function () {
var rate = Number(jQuery('.rate').val());
var per = Number(jQuery('.per').val());
var NPer = Number(jQuery('.NPer').val());
var pmt = Number(jQuery('.pmt').val());
var fv = Number(jQuery('.fv').val());
//var fvTot=fv(rate, per, NPer, pmt, pv);
});
});
// This function is from David Goodman's Javascript Bible.
function conv_number(expr, decplaces) {
var str = "" + Math.round(eval(expr) * Math.pow(10, decplaces));
while (str.length <= decplaces) {
str = "0" + str;
}
var decpoint = str.length - decplaces;
return (str.substring(0, decpoint) + "." + str.substring(decpoint, str.length));
}
// Parameters are rate, total number of periods, payment made each period, future value and type (when payments are due)
function pv(rate, per, nper, pmt, fv) {
nper = parseFloat(nper);
pmt = parseFloat(pmt);
fv = parseFloat(fv);
rate = eval((rate) / (per * 100));
if ((pmt == 0) || (nper == 0)) {
alert("Why do you want to test me with zeros?");
return (0);
}
if (rate == 0) { // Interest rate is 0
pv_value = -(fv + (pmt * nper));
} else {
x = Math.pow(1 + rate, -nper);
y = Math.pow(1 + rate, nper);
pv_value = -(x * (fv * rate - pmt + y * pmt)) / rate;
}
pv_value = conv_number(pv_value, 2);
return (pv_value);
}
function fv(rate, per, nper, pmt, pv) {
nper = parseFloat(nper);
pmt = parseFloat(pmt);
pv = parseFloat(pv);
rate = eval((rate) / (per *...