[英]Math.pow difficulty
當我輸入1000作為投資利率4.25作為月利率,並輸入1作為年份時,為什么我得到的結果是4.384414858452464E11,而不是預期的1043.34?
import java.util.Scanner;
public class FinancialApplicationFutureInvestment_13 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter investment amount: ");
int investmentAmount = input.nextInt();
System.out.println("Enter monthly interest rate: ");
double monthlyInterestRate = input.nextDouble();
System.out.println("Enter number of years");
int numOfYears = input.nextInt();
double futureInvestmentValue = investmentAmount *
(Math.pow(1 + monthlyInterestRate, numOfYears * 12));
System.out.println("Accumulated value is: " + futureInvestmentValue);
double test = Math.pow(1 + monthlyInterestRate, numOfYears * 12);
System.out.println(test);
}
}
每月利率可能需要輸入為0.0425
1 + monthlyInterestRate
是monthlyInterestRate
是原始因子,還是以百分比表示?
嘗試除以一百。
公式是
A = P(1+r/100)^n
所以應該
investmentAmount * (Math.pow(1 + (monthlyInterestRate/100), numOfYears * 12));
好吧,您將1 + 4.25(5.25)計算為每月利率,而不是1+(4.25 / 100)。
您應該在System.out.println中使用強制轉換(雙精度為int)-(int)(futureInvestmentValue * 100)/ 100.0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.