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

qxgroojn  于 2021-07-05  发布在  Java
关注(0)|答案(1)|浏览(583)
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

**

我是否需要添加更多行并更新“term”值?

**

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

ar5n3qh51#

试试这个。

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

相关问题