Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author EDGARROJAS

    (@edgarrojas)

    Hello!

    According to https://gist.github.com/ghalimi/4638848, this is the formula for pv.

    
    function PV(rate, periods, payment, future, type) {
      // Initialize type
      var type = (typeof type === 'undefined') ? 0 : type;
    
      // Evaluate rate and periods (TODO: replace with secure expression evaluator)
      rate = eval(rate);
      periods = eval(periods);
    
      // Return present value
      if (rate === 0) {
        return - payment * periods - future;
      } else {
        return (((1 - Math.pow(1 + rate, periods)) / rate) * payment * (1 +rate * type) - future) / Math.pow(1 + rate, periods);
      }
    }
    

    So you just need to put this function at the top of the formula and bellow it you can use it. example:

    
    function PV(rate, periods, payment, future, type) {
      // Initialize type
      var type = (typeof type === 'undefined') ? 0 : type;
    
      // Evaluate rate and periods (TODO: replace with secure expression evaluator)
      rate = eval(rate);
      periods = eval(periods);
    
      // Return present value
      if (rate === 0) {
        return - payment * periods - future;
      } else {
        return (((1 - Math.pow(1 + rate, periods)) / rate) * payment * (1 +rate * type) - future) / Math.pow(1 + rate, periods);
      }
    }
    
    return PV(0.0521/1,11,[paymenttotal],0,0);
    

    In this example you could replace [paymenttotal] for the field that has the total value of your form.

    Regards!

    • This reply was modified 5 years, 5 months ago by EDGARROJAS.
    • This reply was modified 5 years, 5 months ago by EDGARROJAS.
    Thread Starter randrewsv

    (@randrewsv)

    Hello Edgar
    Thanks you for your reply

    I’m having a hard time implementing this, i’m a newbie in your plugin
    do i need to define the variables?

    (function PV(rate, periods, payment, future, type) {
       // Initialize type
          rate =$$field_rate$$/100;
        periods = $$field_periods$$;
        payment = $$field_payment$$;
        future = $$field_future$$;
        type = $$field_type$$;
      var type = (typeof type === 'undefined') ? 0 : type;
    
      // Evaluate rate and periods (TODO: replace with secure expression evaluator)
    
      // Return present value
      if (rate === 0) {
        return -payment * periods - future;
      } else {
        return (((1 -Math.pow(1 + rate, periods)) / rate) * payment * (1 +rate * type) -future) / Math.pow(1 + rate, periods);
      }
    return PV(0.0521/1,11,[$$field_total$$],0,0);
    })();
    

    i’m getting several errors in console and i dont know what i’m doing wrong
    can you please help me?

    Regards!

    Plugin Author EDGARROJAS

    (@edgarrojas)

    Hello!

    I think it actually should be something like` this:

    function PV(rate, periods, payment, future, type) {
      type = (typeof type === 'undefined') ? 0 : type;  
      if (rate === 0) {
        return -payment * periods - future;
      } else {
        return (((1 - Math.pow(1 + rate, periods)) / rate) * payment * (1 +rate * type) - future) / Math.pow(1 + rate, periods);
      }
      
    }
    return PV($$field_rnField3$$,$$field_rnField4$$,$$field_rnField1$$,$$field_rnField5$$,0);
    

    Regards!

    Thread Starter randrewsv

    (@randrewsv)

    Works perfect
    Thanks You

    kind Regards!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Template request excel PV function’ is closed to new replies.