每年等额本金,计算复利的方法

2023-10-30

最近正在学理财,就顺手写了个复利的计算方法。小记一下 

public class CompoundInterestCalculation {
    public static void main(String[] args) {
        //计算公式V = P(1+i)×[(1+i)^n-1]/i
        //V-终值,P-等额本金,i-收益率,n-期数
        long P = 12000;//每年投入本金12000元,每月1000元;
        float i = 0.2f;//年收益率为20%
        long n = 40;//40年

        long V = CompoundInterest(P,i,n);

        System.out.println("终值为:"+V);

    }
    //计算x的n次方的方法
    public static float SecondPower(float x, long n){
        float res = 1;
        if(x==0){
            res = 0;
        }else if(x>0){
            if(n==0){
                res = x;
            }else if(n>0){
                for (int i=0;i<n;i++){
                        res = res*x;
                }
            }
        }
        return res;
    }
    //计算复利的方法
    //计算公式V = P(1+i)×[(1+i)^n-1]/i
    //V-终值,P-等额本金,i-收益率,n-期数
    public static long CompoundInterest(long P,float i,long n) {
        long V;
        float res;
        float x = 1+i;
        res = SecondPower(x,n);
        V = (long) (P*(1+i)*(res-1)/i);
        return V;
    }
}

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

每年等额本金,计算复利的方法 的相关文章

随机推荐