1

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(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 * 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);
}
MayuriS
  • 337
  • 1
  • 4
  • 17
  • 1
    There is nothing built-in for pv and fv in JS. However it should be a straight forward routine to implement yourself. – techfoobar Jul 02 '15 at 06:45

3 Answers3

4

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);
}
Mariya Davydova
  • 1,353
  • 9
  • 14
  • try to add the essential code to the answer instead of just leaving a link here...please . – Jaffer Wilson Jul 02 '15 at 06:55
  • the link use EVAL function to calculate PV is it fine ? when i searched on google for PV and FV i got the same link and i avoided after seeing use of eval function – MayuriS Jul 02 '15 at 07:01
  • @Mariya Davydova i tried your way but i get PV as N.aN Here's the fiddle https://jsfiddle.net/46sbsxf6/4/ – MayuriS Jul 02 '15 at 07:42
  • @MayuriS You've tried to get data `NPer` from an element with non-existing class `NPer`, while you have an element with class `nper` in your template. Thus you only need to wrote `var NPer = Number(jQuery('.nper').val());` instead of `var NPer = Number(jQuery('.Nper').val());`. And default value for `rate` should be a number, not a division. – Mariya Davydova Jul 02 '15 at 09:42
  • @MayuriS You can also avoid eval, if you are sure that you pass numbers to the functions under consideration (as you do in your jsfiddle). – Mariya Davydova Jul 02 '15 at 09:50
  • @MayuriS I've updated my reply above, just repeating - default value for `rate` should be a number, not a division. JS can't parse a number from arbitrary expression. – Mariya Davydova Jul 02 '15 at 09:53
  • but in excel i get 1,982,835.27 – MayuriS Jul 02 '15 at 09:53
  • yes i tried 0.1066 instead of 0.128/12 still getting Zero – MayuriS Jul 02 '15 at 09:55
  • @MayuriS These functions has a bit different parameters, there are `(rate, per, nper, pmt, fv)`, while excel version uses `(rate, nper, pmt, fv)`. I'll remove `per` from an answer to avoid confusion. – Mariya Davydova Jul 02 '15 at 10:20
  • @MayuriS BTW, when I call PV(0,1282÷12;63;−3872917;1;0) in Excel, I get 176337812.79. – Mariya Davydova Jul 02 '15 at 10:28
  • @MayuriS Ok, I got. You set pmt to 0, as you call `PV(0,1282÷12;63;0;−3872917;1)`. This code assumes that pmt is always not 0. However, you can freely change `if (( pmt == 0 ) || ( nper == 0 )) {` to `if ( nper == 0 ) {`, it won't break anything. – Mariya Davydova Jul 02 '15 at 10:40
  • @Mariya DavyDova yes that i have done before but now i have got solution as you said excel don't use per and inside function we were multiplying rate with "per" so i removed that and i got the exact Answer here the fiddle https://jsfiddle.net/46sbsxf6/11/ – MayuriS Jul 02 '15 at 10:43
  • thnks for your answer and continuous reply I'm accepting you solution as answer so please update it from fiddle so that if in future anybody searching for excel PV and FV can find – MayuriS Jul 02 '15 at 10:48
  • I've updated my answer for the case when you pass string number representations instead of numbers themselves. Please note that type is not used in this version of calculations (and in Excel I get the same result for both types - 0 and 1). – Mariya Davydova Jul 02 '15 at 11:18
  • How it is possible to calculate the PV which depends on FV and FV depends on PV? – artuska Feb 13 '17 at 11:15
2

You are getting NaN because nper = parseFloat(nper);

    var NPer = Number(jQuery('.NPer').val());

should replace with

    var NPer = Number(jQuery('.nper').val());
abarisone
  • 3,707
  • 11
  • 35
  • 54
Jeyanthan
  • 21
  • 1
1

CODE FOR FUNCTION PV

<script language="JavaScript">

<!--

// Function to calculate present value of an investment..

// 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 conv_number(expr, decplaces)

{ // This function is from David Goodman's Javascript Bible.

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));

}

// --></script>

Reference:http://www.mohaniyer.com/old/pvcode.htm

CODE FOR FUNCTION FV

<script language="JavaScript">

<!--

// Function to calculate future value of an investment..

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);

// y = Math.pow(1 + rate, nper);

fv_value = - ( -pmt + x * pmt + rate * x * pv ) /rate;

}

fv_value = conv_number(fv_value,2);

return (fv_value);

}

function conv_number(expr, decplaces)

{ // This function is from David Goodman's Javascript Bible.

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));

}

// --></script>

Reference:http://www.mohaniyer.com/old/fvcode.htm

Jaffer Wilson
  • 7,029
  • 10
  • 62
  • 139
  • Try this and let me know...sir. – Jaffer Wilson Jul 02 '15 at 06:54
  • thanks for reply but after googling i also found this link but i avoided it because of Eval function – MayuriS Jul 02 '15 at 06:57
  • @MayuriS please let me know what is the problem with eval function...please – Jaffer Wilson Jul 02 '15 at 06:59
  • it's because it can execute any JavaScript function from a string. Using it makes it easier for people to inject rogue code into the application. http://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea – MayuriS Jul 02 '15 at 07:03
  • So, just remove it and put your validations there...I think this will help...ok – Jaffer Wilson Jul 02 '15 at 07:04
  • hey when i test the PV formula as shown above for this value -pv(0.1282/12,63,-3872917.00,1) i get alert Why you want to test me with Zero and answer is zero but excel show different Answer :1,982,835.27 – MayuriS Jul 02 '15 at 07:12
  • I am not understanding why it is so.....You can check the demo here: For PV- http://www.mohaniyer.com/old/pv.htm and for FV - http://www.mohaniyer.com/old/fv.htm .....ok – Jaffer Wilson Jul 03 '15 at 02:24
  • by @ Mariya Davydova These functions has a bit different parameters, there are (rate, per, nper, pmt, fv), while excel version uses (rate, nper, pmt, fv) i've found the solution – MayuriS Jul 03 '15 at 08:50
  • Yes @MayuriS I know but I have submitted the reference too... If you have any trouble then you can check from the reference...right.. – Jaffer Wilson Jul 03 '15 at 08:52