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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/choath/article/details/86830240

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

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;
    }
}

猜你喜欢

转载自blog.csdn.net/choath/article/details/86830240