-1

これは複利計算機です。現在のように長い数ではなく、小数点以下 2 桁だけの答えを得る方法を理解できないことを除いて、すべて正常に機能します。コードを見て、修正する必要があるかどうかを提案してください。どうもありがとうございました。

class InvestmentProject{
public double CompoundInterest(double InitialDeposit, double YearlyContribution, double InterestRate, int PeriodsInYr) {

double RateInDecimal = InterestRate/100;
double Value = YearlyContribution/RateInDecimal - YearlyContribution/(RateInDecimal * Math.pow(1 + RateInDecimal, PeriodsInYr));
return (InitialDeposit + Value) * Math.pow(1 + RateInDecimal, PeriodsInYr);
}
}
4

3 に答える 3

1

Java で財務計算、特に比較を行う場合は、float や double を使用しないでください。代わりに BigDecimal を使用してください。その理由を説明する記事を次に示します

于 2012-11-14T22:17:45.000 に答える
0

使用するMath.round(result*100)/100;

ここにも同様の質問があり(Javaで数値を小数点以下n桁に丸める方法)、Javaで丸めを行うその他の方法の概要が示されています。

于 2012-11-14T22:06:38.723 に答える
0

decimalFormat クラスを使用する必要があります

import java.text.DecimalFormat;  

public class Java0605
{
    public static void main (String args[])
    {
        DecimalFormat output = new DecimalFormat("00000");
        System.out.println(output.format(1));
        System.out.println(output.format(12));
        System.out.println(output.format(123));
        System.out.println(output.format(1234));
        System.out.println(output.format(12345));
        System.out.println(output.format(123456));
        System.out.println(output.format(1234567));
        System.out.println();
    }
}
于 2012-11-14T23:00:44.993 に答える