如何计算JS中的PV和FV公式?

时间:2015-07-02 06:37:08

标签: javascript

我想在JavaScript中计算PV和FV,实际上在此之前我在excel中工作,它具有PV和FV功能,所以这些功能对我有帮助,现在我在JavaScript中搜索类似的内容所以请帮忙如果有人在JS中实施了PV和FV公式,我就知道了。

在我试过@Mariya Davydova答案之后的小提琴 https://jsfiddle.net/46sbsxf6/5/

但是将N.aN称为PV,但在excel中我得到答案为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);
}

3 个答案:

答案 0 :(得分:3)

FV和PV的示例实现取自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);
}

答案 1 :(得分:2)

您正在获取NaN,因为nper = parseFloat(nper);

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

应替换为

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

答案 2 :(得分:1)

功能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>

参考:http://www.mohaniyer.com/old/pvcode.htm

功能代码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>

参考:http://www.mohaniyer.com/old/fvcode.htm