无法根据减少的当前值重新计算值
我正在尝试构建一个最低付款计算器,但遇到了无法根据当前每月价值计算最低付款的问题。我认为应该有效...锁定浏览器。我已经注释掉了下面代码中给我带来问题的行。如果有人能伸出援手,我将不胜感激。
<?xml version="1.0" encoding="utf-8"?>
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="1296" minHeight="768"
width="100%" height="408"
backgroundColor="#B9ADFF" >
<fx:Declarations>
<mx:CurrencyFormatter
id="Price" precision="2"
rounding="nearest"
decimalSeparatorTo="."
thousandsSeparatorTo=","
useThousandsSeparator="false"
useNegativeSign="true"
currencySymbol="$"
alignSymbol="left"/>
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
private var myDataProvider:ArrayCollection = new ArrayCollection();
protected function clickHandler(event:MouseEvent):void {
myDataProvider.removeAll();
//Creditor Constants
var c:Number = Number(1); //start counting at
var b1:Number = Number(bal1.text); //initial balance
var r1:Number = Number(apr.text) / 100 / 12;//convert apr to decimal
var m1:Number = Number(mpp.text) / 100; //convert mpp to decimal
var mpp:Number = Number(b1 * m1); //minimum payment by percentage
var ipd:Number = Number(b1 * r1); //interest paid
var ppd:Number = Number(mpp - ipd); //principle paid
while(b1 >= 0) {
myDataProvider.addItem({
"months" : c,
"intBal" : Price.format(b1), //balance
"pPaid" : Price.format(ppd), //principle paid
"intPd" : Price.format(ipd), //interest paid
"minmopmt" : Price.format(mpp) //minimum payment
});
c = (c + 1); //count rows
b1 -= (ppd); // Balance minus Principle Paid
///////// THE PROBLEM LINE IS BELOW /////////////
//mpp = (b1 * m1); //minimum payment by percentage
ipd = (b1 * r1); //Interest Paid
ppd = (mpp - ipd); // Principle Paid
}
}
]]>
</fx:Script>
<s:Button label="Calculate" x="26" y="238"
click="clickHandler(event)" />
<s:TextInput x="22" y="277" id="bal1" restrict="[0-9.\-]" textAlign="right" text="1500"/>
<s:Label x="158" y="287" text="Initial Balance
"/>
<s:TextInput x="22" y="307" id="apr" restrict="[0-9.\-]" textAlign="right" text="15"/>
<s:Label x="158" y="317" text="Annual Percentage Rate (APR)"/>
<s:TextInput x="22" y="337" id="mpp" restrict="[0-9.\-]" textAlign="right" text="2"/>
<s:Label x="158" y="347" text="Minimum Payment Percentage"/>
<mx:DataGrid dataProvider="{myDataProvider}" y="10" id="dg" height="184" x="22">
<mx:columns>
<mx:DataGridColumn dataField="months" headerText="Mo" width="30"/>
<mx:DataGridColumn dataField="intBal" headerText="Balance" width="120"/>
<mx:DataGridColumn dataField="pPaid" headerText="Principle Paid" width="120"/>
<mx:DataGridColumn dataField="intPd" headerText="Interest Paid" width="120"/>
<mx:DataGridColumn dataField="minmopmt" headerText="Min Monthly Pmt" width="120"/>
</mx:columns>
</mx:DataGrid>
</s:Application>
I'm trying to build a minimum payment calculator and am having a problem with the minimum payment not being able to calculate based on the current monthly value. What I think should work...locks up the browser. I've commented out the line that is giving me a problem in the code below. I would appreciate if someone could lend a hand.
<?xml version="1.0" encoding="utf-8"?>
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="1296" minHeight="768"
width="100%" height="408"
backgroundColor="#B9ADFF" >
<fx:Declarations>
<mx:CurrencyFormatter
id="Price" precision="2"
rounding="nearest"
decimalSeparatorTo="."
thousandsSeparatorTo=","
useThousandsSeparator="false"
useNegativeSign="true"
currencySymbol="$"
alignSymbol="left"/>
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
[Bindable]
private var myDataProvider:ArrayCollection = new ArrayCollection();
protected function clickHandler(event:MouseEvent):void {
myDataProvider.removeAll();
//Creditor Constants
var c:Number = Number(1); //start counting at
var b1:Number = Number(bal1.text); //initial balance
var r1:Number = Number(apr.text) / 100 / 12;//convert apr to decimal
var m1:Number = Number(mpp.text) / 100; //convert mpp to decimal
var mpp:Number = Number(b1 * m1); //minimum payment by percentage
var ipd:Number = Number(b1 * r1); //interest paid
var ppd:Number = Number(mpp - ipd); //principle paid
while(b1 >= 0) {
myDataProvider.addItem({
"months" : c,
"intBal" : Price.format(b1), //balance
"pPaid" : Price.format(ppd), //principle paid
"intPd" : Price.format(ipd), //interest paid
"minmopmt" : Price.format(mpp) //minimum payment
});
c = (c + 1); //count rows
b1 -= (ppd); // Balance minus Principle Paid
///////// THE PROBLEM LINE IS BELOW /////////////
//mpp = (b1 * m1); //minimum payment by percentage
ipd = (b1 * r1); //Interest Paid
ppd = (mpp - ipd); // Principle Paid
}
}
]]>
</fx:Script>
<s:Button label="Calculate" x="26" y="238"
click="clickHandler(event)" />
<s:TextInput x="22" y="277" id="bal1" restrict="[0-9.\-]" textAlign="right" text="1500"/>
<s:Label x="158" y="287" text="Initial Balance
"/>
<s:TextInput x="22" y="307" id="apr" restrict="[0-9.\-]" textAlign="right" text="15"/>
<s:Label x="158" y="317" text="Annual Percentage Rate (APR)"/>
<s:TextInput x="22" y="337" id="mpp" restrict="[0-9.\-]" textAlign="right" text="2"/>
<s:Label x="158" y="347" text="Minimum Payment Percentage"/>
<mx:DataGrid dataProvider="{myDataProvider}" y="10" id="dg" height="184" x="22">
<mx:columns>
<mx:DataGridColumn dataField="months" headerText="Mo" width="30"/>
<mx:DataGridColumn dataField="intBal" headerText="Balance" width="120"/>
<mx:DataGridColumn dataField="pPaid" headerText="Principle Paid" width="120"/>
<mx:DataGridColumn dataField="intPd" headerText="Interest Paid" width="120"/>
<mx:DataGridColumn dataField="minmopmt" headerText="Min Monthly Pmt" width="120"/>
</mx:columns>
</mx:DataGrid>
</s:Application>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你已经发现了芝诺悖论。如果利率(付款)始终与余额成正比,则余额永远不会完全为零。假设您总是支付所欠金额的一半:
$100
$50
$25
$12.50
...
$0.00009536 <- 这可能看起来您已经付款完毕,但对于您的 while 循环来说,这仍然满足
b1 > 0 。
您有两个选择:
1. 您可以更改规则,使最低付款额为 2% 或 20 美元,以较高者为准。我认为这就是信用卡的实际作用,顺便说一句,
You have discovered Zeno's paradox. If the rate (payment) is always proportional to the balance, the balance will never go completely to zero. Suppose you always paid half of what you owe:
$100
$50
$25
$12.50
...
$0.00009536 <- that may seem like you're done paying, but to your while loop, thats still satisfies
b1 > 0
.You have two options:
1. you can change the rules so the minimum payment is 2% or $20, whichever is greater. I think that's what credit cards actually do, btw
我的猜测是,您发现自己陷入了无限循环。如果你上面的计算要么使 ppd 为负(因为你要减去它),要么让它变得太小以至于你基本上永远不会让 b1 为零(即你有一个平衡的渐近图),就会发生这种情况。
解决此问题的一种方法是计算行数,并在行数变大时跳出循环。将“while(b1 >= 0)”更改为“while( b1 >= 0 && c < 50 )”或其他内容。
My guess is that you're finding yourself in an endless loop. This can happen if your calculations above either make ppd negative (since you're subtracting it), or make it get small so fast that you essentially never get b1 to zero (i.e., you have an asymptotic graph for the balance).
One way to troubleshoot this would be to count rows and break out of the loop if the number of rows gets large. Change "while(b1 >= 0)" to "while( b1 >= 0 && c < 50 )" or something.
必须有一个最低阈值金额才能结束付款。在你的情况下没有,它正好在你指出的位置。考虑定义最低付款金额。例如,
您还应该考虑将计算的行数限制为 500 左右
There has to be a least threshold amount where the payment has to end. In your situation there is none and it is exactly at the line where you have pointed out. Consider defining a minimum payment amount. e.g.
You should also consider limiting number of rows calculated to like 500 or so