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!

Fixed decimal fields

Status
Not open for further replies.

JonR

Technical User
Feb 21, 2001
22
US
Please help!

I have a simple order form (code pasted below) and I am having trouble making the fields only allowing for 2 decimal places.

For instance, when I calculate 3 x .35 I get 1.049999999999999

Can anyone help, or please let me know what I'm doing wrong? I am a beginner at javascript and perhaps there is also a better way to calculate fields too.

Thanks very much!

Jon

--------------------

<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>

</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>
 
This is a common problem with floating point math. Here's a &quot;quick and dirty&quot; way to handle that:

function calcTotal(form)
{
var total1, total2;

//multiply amount by 100 and round off
total1 = Math.round((StoreOrderForm.Qty1.value * StoreOrderForm.Price1.value) * 100);

//divide total by 100 and discard decimal, add decimal point, and add last 2 digits of total
StoreOrderForm.Total1.value = Math.floor(total1 / 100) + '.' + (total1 % 100);

total2 = Math.round((StoreOrderForm.Qty2.value * StoreOrderForm.Price2.value) * 100);

StoreOrderForm.Total2.value = Math.floor(total2 / 100) + '.' + (total2 % 100);

StoreOrderForm.Subtotal.value = Math.floor((total1 + total2) / 100) + '.' + ((total1 + total2( % 100);
}
 
Thanks so much trollacious! You're awesome.
This has been driving me crazy. I really appreciate your help.

Jon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top