最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

delphi - how to Zero out the balance and add add it to the final payment - Stack Overflow

programmeradmin0浏览0评论

I have $11100.00 for the loan amount with a payment of $199.19. The Rate is 0.192308. I should have 58 payments of $199.19 and the last payment is $199.26.

The total must be $11752.28 but I'm getting 11752.21.

I do understand that is a rounding issue, but still I have to add that 0.07 to that last payment but how?

procedure TForm1.Button1Click(Sender: TObject);
var
  paymentAmount, principle, theSum, theNewBalance, paidInterest, theRate : Extended;
  numberOfPays, myCnt : Integer;
begin
  myCnt := 0;
  theNewBalance := 11100.00;
  theRate := 0.192308;
  paymentAmount := 199.19;
  numberOfPays := 59;
  theSum := 0;
  principle := 0;

  ListBox1.Clear;
  ListBox1.Items.Add( 'Starting balance   :  ' + FloatToStr(theNewBalance) );

  repeat
    mycnt := mycnt + 1;

    paidInterest := theRate/100 * theNewBalance;
    principle := paymentAmount - paidInterest;
    theNewBalance := theNewBalance - principle;

    ListBox1.Items.Add( IntToStr(myCnt) + '  : ' +
      FloatToStr( SimpleRoundTo(paymentAmount,-2)) + ' : ' +
      FloatToStr(SimpleRoundTo(paidInterest,-2)) + ' : ' +
      FloatToStr(SimpleRoundTo(principle,-2)) + ' : ' +
      FloatToStr(SimpleRoundTo(theNewBalance,-2)) );

    theSum := theSum + paymentAmount;

  until (myCnt = numberOfPays) or (theNewBalance = 0.00);

  ListBox1.Items.Add( FloatToStr(theSum) );
end;

I have $11100.00 for the loan amount with a payment of $199.19. The Rate is 0.192308. I should have 58 payments of $199.19 and the last payment is $199.26.

The total must be $11752.28 but I'm getting 11752.21.

I do understand that is a rounding issue, but still I have to add that 0.07 to that last payment but how?

procedure TForm1.Button1Click(Sender: TObject);
var
  paymentAmount, principle, theSum, theNewBalance, paidInterest, theRate : Extended;
  numberOfPays, myCnt : Integer;
begin
  myCnt := 0;
  theNewBalance := 11100.00;
  theRate := 0.192308;
  paymentAmount := 199.19;
  numberOfPays := 59;
  theSum := 0;
  principle := 0;

  ListBox1.Clear;
  ListBox1.Items.Add( 'Starting balance   :  ' + FloatToStr(theNewBalance) );

  repeat
    mycnt := mycnt + 1;

    paidInterest := theRate/100 * theNewBalance;
    principle := paymentAmount - paidInterest;
    theNewBalance := theNewBalance - principle;

    ListBox1.Items.Add( IntToStr(myCnt) + '  : ' +
      FloatToStr( SimpleRoundTo(paymentAmount,-2)) + ' : ' +
      FloatToStr(SimpleRoundTo(paidInterest,-2)) + ' : ' +
      FloatToStr(SimpleRoundTo(principle,-2)) + ' : ' +
      FloatToStr(SimpleRoundTo(theNewBalance,-2)) );

    theSum := theSum + paymentAmount;

  until (myCnt = numberOfPays) or (theNewBalance = 0.00);

  ListBox1.Items.Add( FloatToStr(theSum) );
end;
Share Improve this question edited Mar 13 at 23:56 Remy Lebeau 601k36 gold badges507 silver badges851 bronze badges asked Mar 12 at 15:54 LinwebLinweb 291 silver badge6 bronze badges 3
  • Off-topic, but please format the code better. That makes it nicer and easier to read, and then chances increase that people want to help. – Matthias B Commented Mar 12 at 16:22
  • 2 A solution idea: because rounding errors are inevitable (you can never get rid of them 100% in floating point arithmetic), maybe store your amounts as cents, in Integer variables? – Matthias B Commented Mar 12 at 16:30
  • 2 Hi, please use the correct type when dealing with money, it is called Currency... – whosrdaddy Commented Mar 13 at 10:42
Add a comment  | 

1 Answer 1

Reset to default 3

Rounding errors can, indeed, be a problem in floating point arithmetic, and one sometimes has to resort to using Integers (maybe working with cents) and handling precision oneself.

But in this case, it seems to be a simple error of expectations, and not a rounding problem.

You say that the last payment needs to be $199.26, which is indeed true if one wants the balance to go down to zero, but the program does not contain that. The program uses the same amount, $119.19, for all the payments, including the last (59th) one. So 7 cents too little will have been payed, which is why the sum of payments is 7 cents less than expected (ends with .21 instead of .28).

发布评论

评论列表(0)

  1. 暂无评论