用 Java 编写公式

问题描述 投票:0回答:1

我需要写出这个公式:

我必须用java编写这个公式,但我的版本只显示-0.0。 变量应该全部正确,只是格式不正确。

monthlyPaymentDouble = 
    adjustedPrincipalAmountDouble * monthlyInterestRatePercentageDouble * 
    (Math.pow(1.0 + monthlyInterestRatePercentageDouble,
              totalNumberOfPaymentsInteger) / 
     Math.pow(1.0 + monthlyInterestRatePercentageDouble,
              totalNumberOfPaymentsInteger) - 1.0);

我确信问题出在 math.pow 上,我只是不确定如何让它发挥作用。

java math formula
1个回答
0
投票

为了简洁而重命名变量,并使用

^
来增强功能,您目前拥有:

a * m * ((1 + m) ^ t) / ((1 + m) ^ t) - 1);

由于除法运算符优先于减法运算符,因此相当于:

a * m * (((1 + m) ^ t) / ((1 + m) ^ t)) - 1);
=
a * m * (1 - 1)
=
a * m * 0
=
0

减法需要移入除数:

a * m * ((1 + m) ^ t) / (((1 + m) ^ t) - 1));

将其放回到代码中:

monthlyPaymentDouble = 
    adjustedPrincipalAmountDouble * monthlyInterestRatePercentageDouble * 
    (Math.pow(1.0 + monthlyInterestRatePercentageDouble,
              totalNumberOfPaymentsInteger) / 
     (Math.pow(1.0 + monthlyInterestRatePercentageDouble,
              totalNumberOfPaymentsInteger) - 1.0));
© www.soinside.com 2019 - 2024. All rights reserved.