Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating fields in a form

Status
Not open for further replies.

JonR

Technical User
Feb 21, 2001
22
US
Please help!

I created an order form at .

The fields that calculate are under the Quantity, Price and Total columns. They are set to "No Contstraints." I want to make the Price and Total fields fixed decimals (2 decimal places) because when I try the following examples I get incorrect calculations...

3 X 15 = 0.44999999999999995

3 X .35 = 1.0499999999999998

3 X .55 = 1.6500000000000001

3 X .65 = 1.9500000000000001

3 X .95 = 2.8499999999999996

Has anyone experienced this before? I'd love to be able to show that 3 X .15 = .45, etc..but cannot at this time.

Any help or insight would be much appreciated.

Thanks,

Jon
 
function roundAtTwo(number)
{
var num = Math.round(number * 100);
return parseInt(num) / 100
}

alert(roundAtTwo(0.555555))

Try it it's quite fun :)

Hope this helps. Gary Haran
 
Thanks for your quick reply, Gary.

Please pardon my ignorance (I'm a total novice with Javascript). Do I place this code inside the <HEAD> tags? Do I need to include something to call out the code before each total field?

Thanks again,

Jon
 
I would have to see your code for the page to be able to show you exactly where to implement the function. For now however I can tell you that indeed you would place the function inside the head enclosed within <script> </script> tags.

I wish I could help you more but I would need to see the page for that and currently the link your provided is not showing up. Gary Haran
 
Here is the code of a sample form I created. Thanks again for taking the time to look at this.

<html>
<head>
<title>Test Form</title>

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>

<!-- Begin
function calcTotal(form){
StoreOrderForm.Total1.value = (eval(StoreOrderForm.Qty1.value) * eval(StoreOrderForm.Price1.value));
StoreOrderForm.Total2.value = (eval(StoreOrderForm.Qty2.value) * eval(StoreOrderForm.Price2.value));

StoreOrderForm.Subtotal.value = (eval(StoreOrderForm.Total1.value) + eval(StoreOrderForm.Total2.value));

}
// End -->
</script>



<SCRIPT LANGUAGE=&quot;JavaScript&quot;>


function roundAtTwo(number)
{
var num = Math.round(number * 100);
return parseInt(num) / 100
}
</script>

</head>
<body>

<form method=&quot;POST&quot; name=&quot;StoreOrderForm&quot; action=&quot;--WEBBOT-SELF--&quot;>
<!--webbot bot=&quot;SaveResults&quot;
U-File=&quot; S-Format=&quot;TEXT/CSV&quot;
S-Label-Fields=&quot;TRUE&quot; -->

Qty: <input type=&quot;text&quot; name=&quot;Qty1&quot; size=&quot;20&quot; onChange=&quot;calcTotal(this.form)&quot; value=&quot;0.00&quot;>
Price: <input type=&quot;text&quot; name=&quot;Price1&quot; size=&quot;20&quot; onChange=&quot;calcTotal(this.form)&quot; value=&quot;0.00&quot;>
Total: <input type=&quot;text&quot; name=&quot;Total1&quot; size=&quot;20&quot; value=&quot;0.00&quot; readonly><br>


Qty: <input type=&quot;text&quot; name=&quot;Qty2&quot; size=&quot;20&quot; onChange=&quot;calcTotal(this.form)&quot; value=&quot;0.00&quot;>
Price: <input type=&quot;text&quot; name=&quot;Price2&quot; size=&quot;20&quot; onChange=&quot;calcTotal(this.form)&quot; value=&quot;0.00&quot;>
Total: <input type=&quot;text&quot; name=&quot;Total2&quot; size=&quot;20&quot; value=&quot;0.00&quot; readonly>

<p><b>Total: </b> <input type=&quot;text&quot; name=&quot;Subtotal&quot; size=&quot;20&quot; value=&quot;0.00&quot; readonly></p>


<p><input type=&quot;submit&quot; value=&quot;Submit&quot; name=&quot;B1&quot;><input type=&quot;reset&quot; value=&quot;Reset&quot; name=&quot;B2&quot;></p>
</form>

</body>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top