找不到合适的除法

时间:2019-06-04 21:23:02

标签: java

我检查了另一个“找不到合适的方法”线程,但是它们都不适合除法,所以我不确定从这里开始。

我正在检索用户输入的文本(LoanAmount),并使用它显示每月付款。

我正在尝试显示为期10年的按揭的每月付款。我使用的公式是LoanAmount +(LoanAmount * InterestRate)/120。我意识到,利息计算比这要复杂得多,但是在此示例中,利息没有增加。

我尝试投射LoanAmount。

BigDecimal purchasePrice = new BigDecimal(purchasePriceTextField.getText());
            BigDecimal downPayment = new BigDecimal(downPaymentTextField.getText());
            BigDecimal loanAmount = purchasePrice.subtract(downPayment);
            BigDecimal interestRate = new BigDecimal(interestRateTextField.getText());


            loanAmountTextField.setText(currency.format(loanAmount));
            BigDecimal temp = loanAmount + loanAmount.multiply(interestRate);
            tenYearTextField.setText(currency.format(temp.divide(120, 2, RoundingMode.HALF_UP)));

确切的错误是

MortgageCalculatorController.java:69: no suitable method found for divide(int, int, RoundingMode)
tenYearTextField.setText(currency.format(temp.divide(120, 2, RoundingMode.HALF_UP)));

1 个答案:

答案 0 :(得分:0)

错误表明BigDecimal中没有用参数 int,int,RoundingMode 划分的方法。

您可能要使用的方法是

public BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode)

所以您应该将第69行更改为 tenYearTextField.setText(currency.format(temp.divide(BigDecimal.valueOf(120), 2, RoundingMode.HALF_UP)));

您可能还会在第68行出现错误,这将解决此问题:

BigDecimal temp = loanAmount.add(loanAmount.multiply(interestRate));