MayuriS - 1 year ago 1109
Javascript Question

# How to Calculate PV and FV Formula in JS?

I want to compute PV and FV in JavaScript, actually before this I was working in excel which had a function PV and FV so those function did help me and now I'm searching something like that in JavaScript so please help me out if somebody has implemented PV and FV formula in JS

Here the Fiddle after i tried @Mariya Davydova Answer
https://jsfiddle.net/46sbsxf6/5/

but geting N.aN as PV but in excel i get the answer as 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('.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 * 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
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 are sample implementations of FV and PV, taken from www.mohaniyer.com/old/js.htm.

``````// 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 and 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 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, nper, pmt, pv) {
rate = parseFloat(rate);
nper = parseFloat(nper);
pmt = parseFloat(pmt);
pv = parseFloat(pv);
if ( nper == 0 ) {
alert("Why do you want to test me with zeros?");
return(0);
}
if ( rate == 0 ) { // Interest rate is 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);
}
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download