Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder
    Join Date
    Jan 2004
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Calculation + restriction on value input/output ... HELP!

    I am new to javascript, so please bear with me. I have the following code, which works fine, but when factional input is used ($100.25 vs. $100), I want the resulting calculations to restrict the output to only two place holders after the decimal. I also want to do a javascript prompt if the values entered are not numbers. I have the calculation being done on-the-fly with the onBlur event being used to call the function. I would like to keep this functionality if possible. Thanks in advance for your help!!

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Taxable Gain Tax Calculator</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    <SCRIPT LANGUAGE="JavaScript">
    function dosum(){
    	var inA = parseFloat(document.calc.inA.value);
    	var inB = parseFloat(document.calc.inB.value);
    	var inC = parseFloat(document.calc.inC.value);
    	var inD = parseFloat(document.calc.inD.value);
    	var inE = parseFloat(document.calc.inE.value);
    	var inF = parseFloat(document.calc.inF.value);
    	var outV = (inA-inB-inC+inD-inE);
    	var outW = (inC+inD-inE);
    	var outX = (.25*inE);
    	var outY = (.01*inF)*(inE+inA-inB+inD-inC);
    	var outZ = (inA-inB+inD-inC)*.15;
    	var outTotal = (outX+outY+outZ);
    	document.calc.outV.value = outV;
    	document.calc.outW.value = outW;
    	document.calc.outX.value = outX;
    	document.calc.outY.value = outY;
    	document.calc.outZ.value = outZ;
    	document.calc.outTotal.value = outTotal;
     }
    
    </SCRIPT>
    </head>
    
    <body bgcolor="#FFFFFF" text="#000000" onload="document.calc.inA.focus();">
    <form name="calc">
    <table width="640" border="0" cellspacing="0" cellpadding="0">
    	<tr>
    		<td width="50%" valign="top">
    			<p align="center"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif">Your Data</font></p>
    			<table width="97%" border="1" align="center" cellpadding="3" cellspacing="0">
    				<tr>
    					<td width="55%"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif">A) Sales Price</font></td>
    					<td width="45%"><input name="inA" type="text" value="0" size="16" onblur="dosum();"></td>
    				</tr>
    				<tr>
    					<td><font size="-1" face="Verdana, Arial, Helvetica, sans-serif">B) Cost of Sale</font></td>
    					<td><input name="inB" type="text" value="0" size="16" onblur="dosum();"></td>
    				</tr>
    				<tr>
    					<td><font size="-1" face="Verdana, Arial, Helvetica, sans-serif">C) Original Purchase Price</font></td>
    					<td><input name="inC" type="text" value="0" size="16" onblur="dosum();"></td>
    				</tr>
    				<tr>
    					<td><font size="-1" face="Verdana, Arial, Helvetica, sans-serif">D) Capital Improvements </font></td>
    					<td><input name="inD" type="text" value="0" size="16" onblur="dosum();"></td>
    				</tr>
                    <tr>
    					<td><font size="-1" face="Verdana, Arial, Helvetica, sans-serif">E) Depreciation</font></td>
    					<td><input name="inE" type="text" value="0" size="16" onblur="dosum();"></td>
    				</tr>
                    <tr>
    					<td><font size="-1" face="Verdana, Arial, Helvetica, sans-serif">F) Sale Tax rate (%)</font></td>
    					<td><input name="inF" type="text" value="0" size="16" onblur="dosum();"></td>
    				</tr>
    			</table>
    		</td>
    		<td valign="top">
    			<p align="center"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif">Calculated Values</font></p>
    			<table width="97%" border="1" align="center" cellpadding="3" cellspacing="0">
    				<tr>
    					<td width="55%"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif">V) Capital Gain <br>= A-B-C+D-E</font></td>
    					<td width="45%"><input name="outV" type="text" value="0" size="17" onblur="dosum();"></td>
    				</tr>
    				<tr>
    					<td><font size="-1" face="Verdana, Arial, Helvetica, sans-serif">W) Net Adjusted Basis<br>= C+D-E</font></td>
    					<td><input name="outW" type="text" value="0" size="17" onblur="dosum();"></td>
    				</tr>
    				<tr>
    					<td><font size="-1" face="Verdana, Arial, Helvetica, sans-serif">X) Recaptured Depreciation<br>=.25*E</font></td>
    					<td><input name="outX" type="text" value="0" size="17" onblur="dosum();"></td>
    				</tr>
    				<tr>
    					<td><font size="-1" face="Verdana, Arial, Helvetica, sans-serif">Z) Gain from Appreciation<br>=(A-B+D-C)*.15</font></td>
    					<td><input name="outZ" type="text" value="0" size="17" onblur="dosum();"></td>
    				</tr>
    				<tr>
    					<td><font size="-1" face="Verdana, Arial, Helvetica, sans-serif">Y) State Tax Due<br>=.01*F*(E+A-B+D-C)</font></td>
    					<td><input name="outY" type="text" value="0" size="17" onblur="dosum();"></td>
    				</tr>
    			</table><br><br>
    			<table width="97%" border="1" cellspacing="0" cellpadding="3">
    				<tr>
    					<td width="57%"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif">TOTAL TAXES DUE<br>= X+Y+Z</font></td>
    					<td width="43%"><input name="outTotal" type="text" value="0" size="16" onblur="dosum();"></td>
    				</tr>
    			</table>
    		</td>
    	</tr>
    </table>
    </form>
    </body>
    </html>

  • #2
    Regular Coder
    Join Date
    Jan 2004
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    almost forgot, thanks again!
    Last edited by FastCougar; 01-04-2004 at 07:52 PM.

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Here's a way to round a number with a desired number of decimal digits, using Math.pow function:

    PHP Code:
    <html>
    <
    head>
    <
    script>
    function 
    roundN(){
    val document.forms[0].inp.value;
    nrdig document.forms[0].inp2.value;
    numDec(val,nrdig);
    }
    function 
    numDec(num,dig){          
         
    "";
         if (
    isNaN(num)&&isNaN(dig)) alert('Input numbers, please!!');
         if (
    dig 0)
          {
              
    n=Math.round(num*Math.pow(10dig));
                      for (
    dig i++)
              {
                   
    n%10;
                   
    = (n-d)/10;
                   
    = (d+"")+;
              }
              
    "." t;
              
    n+t;
              }
              else {
    n=num}
              
    document.forms[0].inp3.value n;


    </script>

    </head>
    <body>
    <form>
    Input number <input name="inp" type="text"><br>
    Digits decimal desired 
      <input name="inp2" type="text"><br>
      <br>
      <input type="button" value="Round" onclick="roundN()"><br>
    <br>
    Result
    <input name="inp3" type="text">
    </form>
    </body>
    </html> 
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    sorry
    isNaN(num) || isNaN(dig)
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    Senior Coder coothead's Avatar
    Join Date
    Jan 2004
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,707
    Thanks
    0
    Thanked 237 Times in 232 Posts
    Hi there FastCougar,

    I have made these minor modifications to your script.
    1. .toFixed(2)...will give the results to 2dp
    2. if(isNaN(outTotal)) etc
      will bring up an alert box and then reset the form


    Code:
    <script type="text/javascript">
    function dosum(){
    	var inA = parseFloat(document.calc.inA.value);
                      var inB = parseFloat(document.calc.inB.value);
    	var inC = parseFloat(document.calc.inC.value);
    	var inD = parseFloat(document.calc.inD.value);
    	var inE = parseFloat(document.calc.inE.value);
    	var inF = parseFloat(document.calc.inF.value);
    	var outV = (inA-inB-inC+inD-inE);
    	var outW = (inC+inD-inE);
    	var outX = (.25*inE);
    	var outY = (.01*inF)*(inE+inA-inB+inD-inC);
    	var outZ = (inA-inB+inD-inC)*.15;
    	var outTotal = (outX+outY+outZ);
            document.calc.outV.value = outV.toFixed(2);
            document.calc.outW.value = outW.toFixed(2);
    	document.calc.outX.value = outX.toFixed(2);
    	document.calc.outY.value = outY.toFixed(2);
    	document.calc.outZ.value = outZ.toFixed(2);
    	document.calc.outTotal.value = outTotal.toFixed(2);
                     
                      if(isNaN(outTotal))
     {
                      alert('please enter a number');
                      calc.reset();
     }
     }
    </script>
    I hope that this helps

    cthead

  • #6
    Regular Coder
    Join Date
    Jan 2004
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here is the latest code ... thanks for the .toFixed() fuction. Turns out that the client wanted the calculator to round to the nearest dollar. The .toFixed(0) function works great for rounding up, with one small problem ... it will not round .50 up to 1, it will only round up a majority, so .51 rounds up to 1. Since .50 is dead center in the middle, it decides to round down. Any way to get this to display rounded up if > .49 and round down if <= .49???

    Code:
    <SCRIPT LANGUAGE="JavaScript">
    function dosum(){
    	var inA = parseFloat(document.calc.inA.value);
    	var inB = parseFloat(document.calc.inB.value);
    	var inC = parseFloat(document.calc.inC.value);
    	var inD = parseFloat(document.calc.inD.value);
    	var inE = parseFloat(document.calc.inE.value);
    	var inF = parseFloat(document.calc.inF.value);
    	var outV = (inA-inB-inC+inD-inE);
    	var outW = (inC+inD-inE);
    	var outX = (.25*inE);
    	var outY = ((.01*inF)*(inE+inA-inB+inD-inC));
    	var outZ = ((inA-inB+inD-inC)*.15);
    	var outTotal = (outX+outY+outZ);
    	if(isNaN(inA)){
    		document.calc.inA.value = 'Enter A Number';
    		document.calc.inA.focus();
    		document.calc.inA.select();
    	}
    	if(isNaN(inB)){
    		document.calc.inB.value = 'Enter A Number';
    		document.calc.inB.focus();
    		document.calc.inB.select();
    	}
    	if(isNaN(inC)){
    		document.calc.inC.value = 'Enter A Number';
    		document.calc.inC.focus();
    		document.calc.inC.select();
    	}
    	if(isNaN(inD)){
    		document.calc.inD.value = 'Enter A Number';
    		document.calc.inD.focus();
    		document.calc.inD.select();
    	}
    	if(isNaN(inE)){
    		document.calc.inE.value = 'Enter A Number';
    		document.calc.inE.focus();
    		document.calc.inE.select();
    	}
    	if(isNaN(inF)){
    		document.calc.inF.value = 'Enter A Number';
    		document.calc.inF.focus();
    		document.calc.inF.select();
    	}
    	if(isNaN(outV)){
    		document.calc.outV.value = 'Can\'t Calculate';
    	}else{
    		document.calc.outV.value = outV.toFixed(2);;
    	}
    	if(isNaN(outW)){
    		document.calc.outW.value = 'Can\'t Calculate';
    	}else{
    		document.calc.outW.value = outW.toFixed(2);;
    	}
    	if(isNaN(outX)){
    		document.calc.outX.value = 'Can\'t Calculate';
    	}	else{
    		document.calc.outX.value = outX.toFixed(2);;
    	}
    	if(isNaN(outY)){
    		document.calc.outY.value = 'Can\'t Calculate';
    	}	else{
    		document.calc.outY.value = outY.toFixed(2);;
    	}
    	if(isNaN(outZ)){
    		document.calc.outZ.value = 'Can\'t Calculate';
    	}	else{
    		document.calc.outZ.value = outZ.toFixed(2);;
    	}
    	if(isNaN(outTotal)){
    		document.calc.outTotal.value = 'Can\'t Calculate';
    	}	else{
    		document.calc.outTotal.value = outTotal.toFixed(2);;
    	}
     }
    
    </SCRIPT>
    Note how the error is trapped and the form focuses on the selected error and the tab button will not allow the selection to change focus WORKS GREAT without throwing JS errors via the alert() function.

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    These have the same results:

    var n=4.5;
    alert(n.toFixed(0));
    alert(Math.round(n));

    Both round up to 5. So how come you said 0.5 won't round up to 1?

    I suggest you use Math.random() because toFixed() is not supported in older browsers like IE5.0
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #8
    Regular Coder
    Join Date
    Jan 2004
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by glenngv
    These have the same results:

    var n=4.5;
    alert(n.toFixed(0));
    alert(Math.round(n));

    Both round up to 5. So how come you said 0.5 won't round up to 1?

    I suggest you use Math.random() because toFixed() is not supported in older browsers like IE5.0
    Yes, math.round() does work when called within the alert() function, but when called within my code after the variable assignment, it breaks the functionality of the function. Whenever I try to use math.round(), the form ceases to function as designed. The functionneeds to work based on the onBlur="" call and wrapping everything up in one function was the best solution I could come up with. If you can find a better way, I am all ears. The form must function as follows:

    1) focus on the first field to start
    2) display errors when not entering numbers
    3) auto submit/calculate without pressing buttons
    Last edited by FastCougar; 01-06-2004 at 04:14 AM.

  • #9
    Regular Coder
    Join Date
    Jan 2004
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK, starting from square one ... just talked to the client and he is now requesting that the form allow numbers to allow the use of commas for large integers (i.e. 1,000,000 etc.) and he has also requested that there the form now require pressing a submit button titled "calculate". The submit button I can handle, but getting 1,000 to be recongnized as 1000 is beyond my current knowledge of javascript. Hopefully the new solution will work with rounding using the math.round() funciton. Any help is greatly appreciated!!!

  • #10
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Javascript is case-sensitive. It should be Math.round()
    About the comma, you can use this:
    Code:
    Number.prototype.insertComma = function(){
      var s = this.toString();
      var temp = '';
      for (var i=s.length-1; i>=0; i-=3){
        if ((i-3)>=0) temp = "," + s.substr(i-2, 3) + temp;
        else temp = s.substring(0, i+1) + temp;
      }
      return temp;
    }
    ...
    document.calc.outV.value = Math.round(outV).insertComma();
    And don't tell me that the next time you talk to your client, he would request that there should be trailing ".00" on each currency value
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #11
    Regular Coder
    Join Date
    Jan 2004
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by glenngv
    Javascript is case-sensitive. It should be Math.round()[/code]
    Doh! Now I feel like a complete and utter tool! Thanks for pointing that out ... everything is working great now. The code that you supplied works fine and I understand it now that I see it, but it doesn't solve my problem ... I need to be able to accept comma delimited dollar values, not output them Obviously, with my current code, since I am using parseFloat(), the value is seen as the part of the number before the comma since this function truncates anything following a non numerical character. For example, $10,000 becomes NaN because the dollar sign is used. 10,000 becomes 10 because parseFloat() doesn't recognize the ",". Basically, I need the form to work under the following conditions:

    1) Allow people to enter non-number data associated with money ($ symbol, commas, etc.)
    2) Strip those symbols, run calculations, then re-enter the symbols.
    3) Throw errors within the form's input fields (no alert function calls).

    Attached is the code as I have it currently. Notice that I am formatting the output values using the $ symbol and commas where appropriate ... thanks for that snippet of code ... worked great. Now I just need to allow non-number submittals and then run some logic to detect if the fist character is a dollar sign. If not, is the remainder a number. If so, ignore it and check for the remainder.

    Obviously, I need to treat the input as a string and stop using the parseFloat() function, but how do I go about checking the string for my conditions? Thanks again for everyone's help!!!

    Code:
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    Number.prototype.formatMoney = function(){
    	var s = this.toString();
    	var temp = '';
    	for (var i=s.length-1; i>=0; i-=3){
    		if ((i-3)>=0){
    			temp = "," + s.substr(i-2, 3) + temp;
    		}else{
    			temp = s.substring(0, i+1) + temp;
    		}
    	}
    	temp = '$' + temp;
    	return temp;
    }
    function dosum(){
    	var inA = parseFloat(document.calc.inA.value);
    	var inB = parseFloat(document.calc.inB.value);
    	var inC = parseFloat(document.calc.inC.value);
    	var inD = parseFloat(document.calc.inD.value);
    	var inE = parseFloat(document.calc.inE.value);
    	var inF = parseFloat(document.calc.inF.value);
    	var outV = (inA-inB-inC+inD-inE);
    	var outW = (inC+inD-inE);
    	var outX = (.25*inE);
    	var outY = ((.01*inF)*(inE+inA-inB+inD-inC));
    	var outZ = ((inA-inB+inD-inC)*.15);
    	var outTotal = (outX+outY+outZ);
    	if(isNaN(inA)){
    		document.calc.inA.value = 'Enter A Number';
    		document.calc.inA.focus();
    		document.calc.inA.select();
    	}
    	if(isNaN(inB)){
    		document.calc.inB.value = 'Enter A Number';
    		document.calc.inB.focus();
    		document.calc.inB.select();
    	}
    	if(isNaN(inC)){
    		document.calc.inC.value = 'Enter A Number';
    		document.calc.inC.focus();
    		document.calc.inC.select();
    	}
    	if(isNaN(inD)){
    		document.calc.inD.value = 'Enter A Number';
    		document.calc.inD.focus();
    		document.calc.inD.select();
    	}
    	if(isNaN(inE)){
    		document.calc.inE.value = 'Enter A Number';
    		document.calc.inE.focus();
    		document.calc.inE.select();
    	}
    	if(isNaN(inF)){
    		document.calc.inF.value = 'Enter A Number';
    		document.calc.inF.focus();
    		document.calc.inF.select();
    	}
    	if(isNaN(outV)){
    		document.calc.outV.value = 'Can\'t Calculate';
    	}
    	else{
    		document.calc.outV.value = Math.round(outV).formatMoney();
    	}
    	if(isNaN(outW)){
    		document.calc.outW.value = 'Can\'t Calculate';
    	}
    	else{
    		document.calc.outW.value =  Math.round(outW).formatMoney();
    	}
    	if(isNaN(outX)){
    		document.calc.outX.value = 'Can\'t Calculate';
    	}
    	else{
    		document.calc.outX.value =  Math.round(outX).formatMoney();
    	}
    	if(isNaN(outY)){
    		document.calc.outY.value = 'Can\'t Calculate';
    	}
    	else{
    		document.calc.outY.value =  Math.round(outY).formatMoney();
    	}
    	if(isNaN(outZ)){
    		document.calc.outZ.value = 'Can\'t Calculate';
    	}
    	else{
    		document.calc.outZ.value =  Math.round(outZ).formatMoney();
    	}
    	if(isNaN(outTotal)){
    		document.calc.outTotal.value = 'Can\'t Calculate';
    	}	else{
    		document.calc.outTotal.value =  Math.round(outTotal).formatMoney();
    	}
     }
    -->
    </SCRIPT>
    Last edited by FastCougar; 01-06-2004 at 04:14 PM.

  • #12
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    I have just one clarification.
    What if the user enters something like: $10,0000,0,000,
    Would you still check the position of the commas?
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #13
    Regular Coder
    Join Date
    Jan 2004
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by glenngv
    I have just one clarification.
    What if the user enters something like: $10,0000,0,000,
    Would you still check the position of the commas?
    Very good point! I would think not since that isn't a recognized numerical value in the United States. I would throw an error within the offending field, selected the field, and place focus on the field so that they could correct their mistake.

    Since the nature of this problem changed, I posted a new thread about the input issue I am having. I know on most boards that multiple postings is a no-no, but I see the two threads as solving two different problems. If you care to input on this issue, please refer to this thread. Thanks again for you help

    http://www.codingforums.com/showthre...threadid=31141


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •