Calculate interest rate in Java (TVM)

柔情痞子 提交于 2019-12-13 04:31:05

问题


I've been working on a Java project which is calculator which can be used for calculating different scenarios of compound interest (very similar to the TVM Function found on a graphics calculator like this one)

The main function of the calculator is to calculate missing values using the known values in a formula. I have gotten all of the formulas working except for the one which calculates Interest rate (I)

I have done some research and apparently there is no straight formula to calculate the interest rate. This website: http://www.getobjects.com/Components/Finance/TVM/formulas.html shows the method i need to use, but it requires some iteration to find I using trial and error. (Check the link, Scroll down to the heading "Interest Rate Per Year")

Here is the structure I have set up for it:

public static double calculateI(double N, double PV, double PMT, double FV, double PY){
    //method for calculating I goes here

    return I;
}

I am not sure how to implement this, could someone please suggest how this can be done or point me in the right direction?

Thanks in advance.


Here is my code after the suggestion made by @rocketboy

public static double formulaI(double ip, double N, double PV, double PMT, double FV, double PY){
    double I1=(PV*Math.pow((1+ip),N))+((PMT*1)*(Math.pow((1+ip),N))-1)+FV;
    return I1;
}
public static double calculateI(double N, double PV, double PMT, double FV, double PY){
    double ip=0;
    double res;
    do{
        res = formulaI(ip,N,PV,PMT,FV,PY);
        ip=ip+0.01;
        System.out.println(res);
    }while(res!=0);
    double I=ip*PY;
return I;
}

回答1:


Try something like:

double calculateI(/*all values for varialbles*/){
//definition;
}

Double.valueOf(d).equals(0.0);

long ip =0;
double res;

do{
   res = calculateI(ip, /*other constant values*/);
   ip++; /*Or you can increase ip according to your logic*/    
}while ( Double.valueOf(res).equals(0.0/*Expected result*/));

Edit: You have to handle the edge cases. The equation may not ever converge to 0.



来源:https://stackoverflow.com/questions/18178833/calculate-interest-rate-in-java-tvm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!