计算Java(TVM)的利率 [英] Calculate interest rate in Java (TVM)
问题描述
我一直在研究一个Java项目,它是计算器,可用于计算复合兴趣的不同场景(非常类似于图形计算器上的TVM函数,如这一个)
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)
计算器的主要功能是使用公式中的已知值计算缺失值。除了计算利率( I )
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)
我已经做了一些研究,显然有没有直接的公式来计算利率。本网站: http://www.getobjects.com/Components/Finance/TVM/ formulas.html 显示了我需要使用的方法,但需要一些迭代才能使用反复试验找到 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?
提前致谢。
以下是@rocketboy建议后的代码
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;
}
推荐答案
尝试类似:
double calculateI(/*all values for varialbles*/){
//definition;
}
Double.valueOf(d).equals(0.0);
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*/));
编辑:您必须处理边缘情况。等式可能永远不会收敛到0。
You have to handle the edge cases. The equation may not ever converge to 0.
这篇关于计算Java(TVM)的利率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!