无法根据减少的当前值重新计算值

发布于 2024-09-30 10:39:21 字数 3425 浏览 0 评论 0原文

我正在尝试构建一个最低付款计算器,但遇到了无法根据当前每月价值计算最低付款的问题。我认为应该有效...锁定浏览器。我已经注释掉了下面代码中给我带来问题的行。如果有人能伸出援手,我将不胜感激。

<?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&#xd;"/>      
<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

强者自强 2024-10-07 10:39:22

你已经发现了芝诺悖论。如果利率(付款)始终与余额成正比,则余额永远不会完全为零。假设您总是支付所欠金额的一半:

$100

$50

$25

$12.50

...

$0.00009536 <- 这可能看起来您已经付款完毕,但对于您的 while 循环来说,这仍然满足 b1 > 0 。

您有两个选择:
1. 您可以更改规则,使最低付款额为 2% 或 20 美元,以较高者为准。我认为这就是信用卡的实际作用,顺便说一句,

  1. 当你的余额低于 1 美分时,你可以停止使用。

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

  1. You can stop when your balance gets to be less than 1 cent.
梦醒灬来后我 2024-10-07 10:39:21

我的猜测是,您发现自己陷入了无限循环。如果你上面的计算要么使 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.

雅心素梦 2024-10-07 10:39:21

必须有一个最低阈值金额才能结束付款。在你的情况下没有,它正好在你指出的位置。考虑定义最低付款金额。例如,

var minimumPaymentAmount = 1;

....

if(b1 * m1 < minimumPaymentAmount) 
  mpp =  minimumPaymentAmount;     
else
  mpp =  b1 * m1; 

您还应该考虑将计算的行数限制为 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.

var minimumPaymentAmount = 1;

....

if(b1 * m1 < minimumPaymentAmount) 
  mpp =  minimumPaymentAmount;     
else
  mpp =  b1 * m1; 

You should also consider limiting number of rows calculated to like 500 or so

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文