How is present value (PV) calculated on Google She

2019-07-18 02:58发布

问题:

The PV formula on Google Sheets can take 3, 4, or 5 parameters. I am trying to calculate the present value given the parameters in JavaScript but cannot seem to figure out or find the formula that Google Sheets is using.

The parameters being passed into the function are currently =PV(.00115, 7.57, $66.88) and the output from the function is $503.89. How is this being calculated? I need to either replicate this formula in JavaScript or find some type of library with a function that will do this for me.

回答1:

After consulting Wikipedia I came up with this function, which returns identical result to built-in PV. Tested by entering it as a custom function and throwing some random values at it.

function myPV(rate, num, amount, remain, type) {
  if (type == 0) {
    var factor = (rate == 0 ? num : (1 - Math.pow(1+rate, -num))/rate);
    return -amount*factor - remain * Math.pow(1+rate, -num);
  }
  else {
    var factor = (rate == 0 ? num : (1 - Math.pow(1+rate, -num))*(1+rate)/rate);
    return -amount*factor - remain * Math.pow(1+rate, -num);
  }
}