如何计算每年,半年,季度,每月和每天的复利?

时间:2020-09-04 05:15:53

标签: java

public class interest {

    public void calculate(int principle, int year, double interestRate, int terms) {
        double sinterest = ((500000 * 3.5 * 10) / 100);
        double amount = principle * Math.pow(1 + (interestRate / terms), terms * year);
        double cinterest = amount - principle;

        System.out.println("Simple interest on Taka. 500000.00 in " + year + " years = Taka "+sinterest);
        System.out.println("Interest on Taka. 500000.00 in " + year + " years compounded annually = Taka. "+cinterest);
        System.out.println("Interest on Taka. 500000.00 in " + year + " years compounded semi-annually = Taka. "+cinterest);
        System.out.println("Interest on Taka. 500000.00 in " + year + " years compounded quarterly = Taka. "+cinterest);
        System.out.println("Interest on Taka. 500000.00 in " + year + " years compounded monthly = Taka. "+cinterest);
        System.out.println("Interest on Taka. 500000.00 in " + year + " years compounded daily = Taka. "+cinterest);


        
    }
    public static void main(String args[]) {
        interest obj = new interest();
        obj.calculate(500000, 10, 0.035, 4); //principle: 500000, year: 10, interest: 0.035, terms: 4
    }
}

当前输出:

Simple interest on Taka. 500000.00 in 10 years = Taka 175000.0
Interest on Taka. 500000.00 in 10 years compounded annually = Taka. 208454.41896556818
Interest on Taka. 500000.00 in 10 years compounded semi-annually = Taka. 208454.41896556818
Interest on Taka. 500000.00 in 10 years compounded quarterly = Taka. 208454.41896556818
Interest on Taka. 500000.00 in 10 years compounded monthly = Taka. 208454.41896556818
Interest on Taka. 500000.00 in 10 years compounded daily = Taka. 208454.41896556818

所有复利(每年,半年,季度,每月和每天)打印相同的值(每季度=塔卡208454.42)。它应该与预期输出不同。

预期输出:

Simple interest on Taka. 500000.00 in 10 years = Taka. 175000.00
Interest on Taka. 500000.00 in 10 years compounded annually = Taka. 205299.38
Interest on Taka. 500000.00 in 10 years compounded semi-annually = Taka. 207389.10
Interest on Taka. 500000.00 in 10 years compounded quarterly = Taka. 208454.42
Interest on Taka. 500000.00 in 10 years compounded monthly = Taka. 209172.41
Interest on Taka. 500000.00 in 10 years compounded daily = Taka. 209521.87

**

我是否必须添加更多这一行并更新“条款”值?

**

obj.calculate(500000, 10, 0.035, 4); //principle: 500000, year: 10, interest: 0.035, terms: 4

1 个答案:

答案 0 :(得分:0)

尝试一下。

public void calculate(int principle, int year, double interestRate) {
    double sinterest = principle * interestRate * year;
    Function<Integer, Double> interest = terms -> principle * Math.pow(1 + (interestRate / terms), terms * year) - principle;

    System.out.printf("Simple interest on Taka. 500000.00 in %d years = Taka %.2f%n", year, sinterest);
    System.out.printf("Interest on Taka. 500000.00 in %d years compounded annually = Taka. %.2f%n", year, interest.apply(1));
    System.out.printf("Interest on Taka. 500000.00 in %d years compounded semi-annually = Taka. %.2f%n", year, interest.apply(2));
    System.out.printf("Interest on Taka. 500000.00 in %d years compounded quarterly = Taka. %.2f%n", year, interest.apply(4));
    System.out.printf("Interest on Taka. 500000.00 in %d years compounded monthly = Taka. %.2f%n", year, interest.apply(12));
    System.out.printf("Interest on Taka. 500000.00 in %d years compounded daily = Taka. %.2f%n", year, interest.apply(365));
}

obj.calculate(500000, 10, 0.035); //principle: 500000, year: 10, interest: 0.035

输出:

Simple interest on Taka. 500000.00 in 10 years = Taka 175000.00
Interest on Taka. 500000.00 in 10 years compounded annually = Taka. 205299.38
Interest on Taka. 500000.00 in 10 years compounded semi-annually = Taka. 207389.10
Interest on Taka. 500000.00 in 10 years compounded quarterly = Taka. 208454.42
Interest on Taka. 500000.00 in 10 years compounded monthly = Taka. 209172.41
Interest on Taka. 500000.00 in 10 years compounded daily = Taka. 209521.87
相关问题