如何在Java中计算Excel的PV函数

huangapple go评论71阅读模式
英文:

How to calculate Excel PV function in Java

问题

这是一个用于计算现值(PV)的Excel公式

calculatePV(double rate, int nper, double pmt, double fv, int type)

我如何在Java中使用给定的参数进行相同的计算?

英文:

This is an excel formula to calculate Present Value (PV)

PV (rate, nper, pmt, [fv], [type])

How do I calculate the same in Java with the mentioned parameters?

答案1

得分: 1

public static double pv(double rate, double numberOfPeriod, double payment) {
    double retval = 0;
    boolean t = false;
    if (rate == 0) {
        retval = -1*((numberOfPeriod*payment));
    }
    else {
        double r1 = rate + 1;
        retval =
            (( ( 1 - Math.pow(r1, numberOfPeriod) ) / rate ) * (t ? r1 : 1)  * payment - 0) 
            /
            Math.pow(r1, numberOfPeriod);
    }
    return retval;
}

其中,t 是类型 - 支付时机。0 = 期末,1 = 期初。默认值为 0。

英文:
public static double pv(double rate, double numberOfPeriod, double payment) {
    double retval = 0;
    boolean t = false;
    if (rate == 0) {
        retval = -1*((numberOfPeriod*payment));
    }
    else {
        double r1 = rate + 1;
        retval =
        		(( ( 1 - Math.pow(r1, numberOfPeriod) ) / rate ) * (t ? r1 : 1)  * payment - 0) 
        		/
                Math.pow(r1, numberOfPeriod);
    }
    return retval;
}

where t is type - When payments are due. 0 = end of period, 1 = beginning of period. The default is 0.

huangapple
  • 本文由 发表于 2020年9月3日 14:18:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63717901.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定