admin管理员组文章数量:1391960
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
1 Answer
Reset to default 3Rounding 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).
本文标签: delphihow to Zero out the balance and add add it to the final paymentStack Overflow
版权声明:本文标题:delphi - how to Zero out the balance and add add it to the final payment - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744742858a2622712.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论