Attempting to calculate monthly repayments with BigDecimal calculations

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I am a beginner attempting to create a small program in Java that allows a user to input their loan term, interest rate etc. and will hopefully spit out an estimate for monthly repayments. When i am doing this, however, it gives me a completely different number to what i've calculated manually.

I been told to use the BigDecimal math system as the primitive double data type is not accurate enough for financial calculations.

The steps i am doing are attempting to mimic this formula : M = P [i(1+i)^n/ 1-(1+i)^n]

BigDecimal iRateInput = BigDecimal.valueOf(iRate);
BigDecimal twelve = new BigDecimal("12"); 
BigDecimal iRateMonthly = iRateInput.divide(twelve); 
BigDecimal one = new BigDecimal ("1"); 
BigDecimal iRateTemp = iRateMonthly.add(one);
BigDecimal loanTermBD = BigDecimal.valueOf(loanTerm);
loanTermBD = loanTermBD.multiply(twelve); 
BigDecimal iRatePower = iRateTemp.pow(loanTerm); 
BigDecimal iRateTop = iRateMonthly.multiply(iRatePower);
BigDecimal iRateBottom = iRatePower.subtract(one);
BigDecimal iRateTotal = iRateTop.divide(iRateBottom, BigDecimal.ROUND_UP);
BigDecimal borrowAmountBD = BigDecimal.valueOf(borrowAmount);
BigDecimal repayments = borrowAmountBD.multiply(iRateTotal);

I have been testing this code with:

P = 100,000

n = 15 years (*12)

r = 0.06p/a

According to my calculations, the monthly repayment should be 843.86 but I am getting 6936.43

Help would be appreciated! <3

profilepic.png
manpreet 2 years ago
private static double calculateRatePower(double rate, int period) {
  return Math.pow(1.0 + rate/12.0, period * 12.0);
}

public static void main(String[] args) throws IOException {
  double iRate = 0.06;
  double borrowAmount = 100000.0;
  int loanTerm = 15;

  double top = (iRate / 12.0) * calculateRatePower(iRate, loanTerm);
  double bottom = 1 - calculateRatePower(iRate, loanTerm);

  double repayments = borrowAmount * (top/bottom);
  System.out.println(repayments);
}

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.