Results 1 to 3 of 3

Thread: Calculation using javascript

  1. #1
    Join Date
    Nov 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow Calculation using javascript

    hi.,

    i need to calculate two text box values .... and value to be displayed in the third text box .....


    i m using VS 2008.....

    my my form is present inside the Master Page...

    how can i do it by using the JAva Script....

    Cau u pls help me ....

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    We really would need more info, perhaps a link to your page would help - that depends upon how far along you are, and how good we are at guessing what you want/need.

    Failing that as a useful guide, I would suggest taking a few deep breaths and then describing in detail what you want.

    The operators for addition (+), subtraction (-), multiplication (*) and division (/) in javascript are common and explanations of them are easily found using Google.

    The one thing that often isn't made so clear, especially as regards form values when used in javascript math is that (depending upon the operation) one must often make sure that the value is a number and/or convert it to one. If it is a number but expressed as a string (as all form values are), results will often be other than expected, particularly where addition is involved.

    The easiest way to convert a string value that is a number to an actual javascript number is to subtract 0 from it. If the value contains numbers and text (assuming base 10, that would be letters or punctuation other than a single . decimal point), regular expression processing or the parseInt() method must be used to convert it to an actual javascript number. With regular expression processing, if the operator is (+) addition, one must still then subtract 0 from it to get an actual javascript number before working with it further using addition.
    Last edited by jscheuer1; 11-26-2008 at 06:42 AM. Reason: word order
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Oct 2008
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Ello i had this laying about on my hard drive after i did some messing with JS a while ago. I warn you tho im really crap at JS so its probly badly formed and full of errors but it works so maybe you can gleam some info from it.

    Hope it helps.

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Simple Calculations Example</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script language="JavaScript" type="text/javascript">
    function calculateTotal_multiply (form) { 
    	var numeratorMultiply = form.numeratorMultiply.value;
    	var demoninatorMultiply = form.demoninatorMultiply.value;
    	var total = (numeratorMultiply * demoninatorMultiply);
    	form.boxtotalMultiply.value = (total);
    	}
    function calculateTotal_Add (form) { 	
    	var numeratorAdd = +form.numeratorAdd.value;						
    	var demoninatorAdd = +form.demoninatorAdd.value;
    	var total = (numeratorAdd + demoninatorAdd);		
    	form.boxtotalAdd.value = (total);
    	}
    function calculateTotal_Sub (form) { 	
    	var numeratorSub = form.numeratorSub.value;	
    	var demoninatorSub = form.demoninatorSub.value;
    	var total = (numeratorSub - demoninatorSub);
    	form.boxtotalSub.value = (total);
    	}
    function calculateTotal_Div (form) { 
    	var numeratorDiv = form.numeratorDiv.value;	
    	var demoninatorDiv = form.demoninatorDiv.value;	
    	var total = (numeratorDiv / demoninatorDiv);
    	form.boxtotalDiv.value = (total);
    	}
    function calculateTotalTotals (form) { 	
    	var totalMulti = +form.boxtotalMultiply.value;	
    	var totalAdd = +form.boxtotalAdd.value;	
    	var totalSub = +form.boxtotalSub.value;	
    	var totalDiv = +form.boxtotalDiv.value;	
    	var total = (totalMulti + totalAdd + totalSub + totalDiv); 
    	form.boxtotalTotals.value = (total);
    	}
    function roundNumber(num, dec) {
    	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec); 
    	return result;
    	}
    function doVAT (form) {
    	var subTotal = +form.vatSubTotal.value;
    	var vatTotal1 = (subTotal / 100 * 17.5)
    	var vatTotal2 = (subTotal + vatTotal1);
    	var roundVAT = roundNumber (vatTotal2,2);
    	form.vatTotal2.value = (roundVAT)
    	}
    </script>
    </head>
    
    <body style="font-family: arial; font-size: 12px;"> 
    <p><script>document.write(total);</script></p>
    Multiply Example:<br /><form method="get" action="" name="form">
    <input type="text" name="numeratorMultiply" value=""> x <input type="text" name="demoninatorMultiply" value="">
    <input type="button" value="Submit" onClick="calculateTotal_multiply(this.form)">
    = <input name="boxtotalMultiply" type="text" value="">
    <br />
    Addition Example:<br />
    <input type="text" name="numeratorAdd" value=""> + <input type="text" name="demoninatorAdd" value="">
    <input type="button" value="Submit" onClick="calculateTotal_Add(this.form)">
    
    
    = <input name="boxtotalAdd" type="text" value="">
    <br />
    Subtraction Example:<br />
    <input type="text" name="numeratorSub" value=""> - <input type="text" name="demoninatorSub" value="">
    <input type="button" value="Submit" onClick="calculateTotal_Sub(this.form)">
    = <input name="boxtotalSub" type="text" value="">
    <br />
    Division Example:<br />
    <input type="text" name="numeratorDiv" value=""> / <input type="text" name="demoninatorDiv" value="">
    
    <input type="button" value="Submit" onClick="calculateTotal_Div(this.form)">
    = <input name="boxtotalDiv" type="text" value="">
    <br /><br />
    <input type="button" value="Add Totals Together" onClick="calculateTotalTotals(this.form)">&nbsp;<input type="text" name="boxtotalTotals">
    </form>
    <form method="get" action="" name="vat">
    VAT @ 17.5% <br />
    <input type="text" name="vatSubTotal" value="">
    <input type="button" value="Do VAT @ 17.5%" onClick="doVAT(this.form)">=
    <input type="text" name="vatTotal2" value="">
    </form>
    </body>
    </html>

Bookmarks

Posting Permissions

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