[英]Math.pow difficulty
When I enter 1000 for investment amount 4.25 for monthly interest rate and 1 for years, why do I get the result 4.384414858452464E11 instead of the expected 1043.34? 当我输入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
Is monthlyInterestRate
a raw factor, or is it expressed in percentage points? 是monthlyInterestRate
是原始因子,还是以百分比表示?
Try dividing by one hundred. 尝试除以一百。
the formula is 公式是
A = P(1+r/100)^n
so it should be 所以应该
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.