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 5 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

Write Your Comments or Explanations to Help Others



Tuteehub forum answer Answers (2)


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 m.tuteehub.com/tag/term">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 m.tuteehub.com/tag/calculations">calculations.

The steps i am doing are attempting to mimic this m.tuteehub.com/tag/formula">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 m.tuteehub.com/tag/calculations">calculations, the monthly repayment should be 843.86 but I am getting 6936.43

Help would be appreciated! <3

0 views   0 shares
profilepic.png
manpreet 2 years ago

The first two types Java (double float) with a floating point number is stored as a binary representation of the exponent part. Some of the first race (unless string) have a fixed number of points. Unlike a number of floating point numbers of all times, an error reply small (about 10 ^ -19) Finally, why 0.009999999999999998 0.04-0.03 from the above example.

However BigDecimal provides us with the exact answer. so try with long and decimal first and check with small sample data first and identify the issue if its wrong.Hope this help u!


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.

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community