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

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

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

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

希文先森

您的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值