Web Analytics Made Easy -
StatCounter Calculation coding assistance re: Interest - CodingForum

Announcement

Collapse
No announcement yet.

Calculation coding assistance re: Interest

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Calculation coding assistance re: Interest

    I've been working on this for a few days now and I'm not sure if it could be done or not. I'm trying to do the following complex formula in Adobe Acrobat. I want to be able to calculate tInterest (Total Interest Due) = fDays (# of days) * .0002739726 (interest rate) * PrincipalAmt (principal) but ONLY IF fDays > 90 or if CheckBox1 is checked then fDays > 180, otherwise tInterest = 0.

    See, I'm trying to create a form where the interest is only to be calculated if 90 days has elapsed OR 180 days has elapsed if checked off.Either way, interest is to be calculated from StartDate.

    Is this possible?

  • #2
    Originally posted by TM921 View Post
    Is this possible?
    Very likely.

    You want something like this:-

    Code:
    var tInterest = 0;  // the default
    if ((fdays >= 90) && (fdays <=180)) {
    tInterest = fdays * .0002739726 * PrincipalAmt ;
    }
    else if ((fdays > 180) && (document.getElementById("checkBox1").checked)) {
    tInterest = fdays * .0002739726 * PrincipalAmt ;
    }
    tInterest = tInterest.toFixed(2);  // result to 2 decimal places

    Quizmaster: Complete the name of the Shakespeare play "Troilus and ....."?
    Contestant: Er... John?
    Last edited by Philip M; Apr 15, 2009, 02:51 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

    Comment


    • #3
      Thank you for that quick reply. I used this code and it is not working. Any suggestions? I did doublecheck the syntax.

      Comment


      • #4
        Not a clue, although I see nothing wrong. You need to be more specific.

        Use alerts at appropriate places to check out what is happening.

        I asume that you have wrapped the code in a function which is called by ??

        All the code given in this post has been tested and is intended to address the question asked.
        Unless stated otherwise it is not just a demonstration.

        Comment


        • #5
          maybe in the mathmatics part it's not set up properly to give accurate total?

          Not 100% on the breakdown but

          Take your deposit amount = P
          interest rate = R
          Times Interest is compounded = N
          number of years = T

          so the formula would be something like


          P(1+R/N) ^(N*T)


          So $5000 at 2.42 interest, that accumulates every 3 months for 1 year would be

          5000(1+ .0242/4) ^ (4*1) = $5122.10 so i that 90 days you made $122.10 in interest

          Comment


          • #6
            I assumed (following the OP's proposed calculation) that it was just simple interest as it appears that the loan is for days, not years.

            It is not much help to say that "the code is not working". You need to be more specific. Do you mean it returns an incorrect calculation? Or that it does not work at all? If so, what error messages do you get?

            All the code given in this post has been tested and is intended to address the question asked.
            Unless stated otherwise it is not just a demonstration.

            Comment


            • #7
              Sorry I wasn't more specific. I don't get any error message. I'm trying to convert an excel spreadsheet into a form. In the field in the spreadsheet, the formula is as follows:

              =0.0002739726*F25*F27

              F25 = number of days
              F27 = principal amount

              The difference is that I want the interest calculated only if 90 days has elapsed and if a checkbox is checked, only if 180 days has elapsed.

              What would the script if I only wanted to put down the 90 day lapse? Maybe I'll start with that and try to work my way up?

              Comment


              • #8
                As far as I can see there is nothing wrong with the Javascript code I provided for you. but of course you have to ensure that the variable names match.


                Here is a more extended version which seems to work perfectly according to your (slightly odd)specification:-


                Code:
                PRINCIPAL AMOUNT <input type = "text" id = "prin">
                DAYS <input type = "text" id = "days">
                <input type = "button" id = "but1" value = "Calculate Interest" onclick = "calculate()"><br><br>
                CHECK HERE FOR SOMETHING <input type = "checkbox" id = "checkBox1"><br><br>
                INTEREST <input type = "text" id = "result">
                
                <script type = "text/javascript">
                
                function calculate(){
                var tInterest = 0;  // the default
                var fdays = document.getElementById("days").value;
                var PrincipalAmt = document.getElementById("prin").value;
                if ((fdays >= 90) && (fdays <=180)) {
                tInterest = fdays * .0002739726 * PrincipalAmt ;
                }
                else if ((fdays > 180) && (document.getElementById("checkBox1").checked)) {
                tInterest = fdays * .0002739726 * PrincipalAmt ;
                }
                tInterest = tInterest.toFixed(2);  // result to 2 decimal places
                document.getElementById("result").value = tInterest;
                }
                
                </script>
                I don't really understand why no interest is charged if the checkbox is not checked. Are you really trying to say that the minimum loan period is 90 or 180 days?
                Last edited by Philip M; Apr 16, 2009, 08:10 AM.

                All the code given in this post has been tested and is intended to address the question asked.
                Unless stated otherwise it is not just a demonstration.

                Comment


                • #9
                  Thanks for your help.

                  I tried running that script and I'm getting the following error message:

                  missing ; before statement
                  1: at line 2


                  Yes, the form I'm working on is for State District Courts to calculate interest due on a bond forfeiture. If a Defendant fails to appear for court and a bond was posted, the bondsman/surety has 90 days to pay the forfeiture. If they wait until day #91 or later, interest is then charged from the fail to appear date. If they file for an extension and it is granted, they get 180 days to pay the forfeiture interest-free.

                  Comment


                  • #10
                    Try this:
                    Code:
                    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
                    <html>
                    <head>
                    <title>Form</title>
                    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
                    <script type="text/javascript">		
                    
                    	var today = new Date();
                    	today.setHours(0,0,0,0);
                    
                    	function validate(isField){
                    
                    		var splitDate = isField.value.split("/");
                    		var refDate = new Date(isField.value);
                    		if (splitDate[0] < 1 || splitDate[0] > 12 || refDate.getDate() != splitDate[1] || splitDate[2].length != 4 || (!/^20/.test(splitDate[2])))
                    			{
                    			 alert('Invalid Date')
                    			 isField.value = ""; 
                    			 isField.focus();
                    			 return false;
                    			}	
                    		return refDate;
                    	}
                    
                    	function calculate(nForm){
                    
                    		var endDate = today;
                    		var startDate = validate(nForm['noShow']);
                    		if (startDate)
                    			{
                    			 if (startDate > endDate){alert('Invalid Failure to Appear Date'); return false;}
                    			 var daysElapsed = Math.round((endDate-startDate)/86400000);
                    			 nForm['daysDelinquent'].value = daysElapsed;
                    			}
                    		else {return false;}
                    		var interest = nForm['interestDue'];
                    		var forfeiture = nForm['forfeitureAmt'].value;
                    		if (nForm['extension'].checked && daysElapsed > 180 || !nForm['extension'].checked && daysElapsed > 90)
                    			{
                    			 interest.value = (forfeiture * .0002739726 * daysElapsed).toFixed(2)
                    			}
                    		else 	{
                    			 interest.value = "None"; 			
                    			}
                    	}
                    	
                    </script>
                    </head>
                    	<body>
                    		<form action="">
                    
                    			<label>Forfeiture Amount: $<input type="text" name="forfeitureAmt" size="12"></label>
                    			<br><br>
                    			<label>Failure to Appear Date (mm/dd/yyyy): <input type="text" name="noShow" size="12"></label>
                    			<br><br>
                    			<label>Extension Granted: <input type="checkbox" name="extension"></label>
                    			<br><br>
                    			<label>Days Delinquent: <input type="text" name="daysDelinquent" size="12" readonly></label>
                    			<br><br>
                    			<label>Interest Due: <input type="text" name="interestDue" size="12" readonly></label>
                    			<br><br>
                    			<input type="button" name="submit" value="Calculate" onclick="calculate(this.form)">
                    		  
                    		</form>
                    	</body>
                    </html>
                    Last edited by 12 Pack Mack; Apr 16, 2009, 11:11 AM.

                    Comment


                    • #11
                      Originally posted by TM921 View Post
                      Thanks for your help.

                      I tried running that script and I'm getting the following error message:

                      missing ; before statement
                      1: at line 2
                      That does not sound like a JavaScript error message. Are you sure you are not using VBScript?

                      All the code given in this post has been tested and is intended to address the question asked.
                      Unless stated otherwise it is not just a demonstration.

                      Comment


                      • #12
                        I'm using Javascript within an Adobe Acrobat document.

                        Comment


                        • #13
                          I am a noob as well and am on here learning for a form I am enhancing in Adobe Acrobat. The code you were provided by Phillip was the entire HTML code, so depending on where you are adding the code to Acrobat you may or may not need the entirety of what was posted.

                          More than likely you only need to copy from "function" through to the last "}" and paste that into the javascript editor.

                          Comment

                          Working...
                          X