javascript - How to Calculate PV and FV Formula in JS? -
i want compute pv , fv in javascript, before working in excel had function pv , fv function did me , i'm searching in javascript please me out if has implemented pv , fv formula in js
here fiddle after tried @mariya davydova answer https://jsfiddle.net/46sbsxf6/5/
but geting n.an pv in excel answer 1,982,835.27
<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" /> 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); }); }); // function 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 rate, total number of periods, payment made each period, future value , type (when payments 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 want test me zeros?"); return (0); } if (rate == 0) { // interest rate 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 * 100)); if ((pmt == 0) || (nper == 0)) { alert("why want test me zeros?"); return (0); } if (rate == 0) { // interest rate 0 fv_value = -(pv + (pmt * nper)); } else { x = math.pow(1 + rate, nper); fv_value = -(-pmt + x * pmt + rate * x * pv) / rate; } fv_value = conv_number(fv_value, 2); return (fv_value); }
there sample implementations of fv , pv, taken www.mohaniyer.com/old/js.htm.
// function 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 rate, total number of periods, payment made each period , future value function pv(rate, nper, pmt, fv) { rate = parsefloat(rate); nper = parsefloat(nper); pmt = parsefloat(pmt); fv = parsefloat(fv); if ( nper == 0 ) { alert("why want test me zeros?"); return(0); } if ( rate == 0 ) { // interest rate 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, nper, pmt, pv) { rate = parsefloat(rate); nper = parsefloat(nper); pmt = parsefloat(pmt); pv = parsefloat(pv); if ( nper == 0 ) { alert("why want test me zeros?"); return(0); } if ( rate == 0 ) { // interest rate 0 fv_value = -(pv + (pmt * nper)); } else { x = math.pow(1 + rate, nper); fv_value = - ( -pmt + x * pmt + rate * x * pv ) /rate; } fv_value = conv_number(fv_value,2); return (fv_value); }
Comments
Post a Comment