java 输入利率,计算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)

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;

}

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值