...

# Auto Whole Number Help

user55
07-18-2007, 06:16 PM
Hi,

I hope somebody can help me with this. I have a form that auto calculates a number of fields. I want to get the last field which is "field4" to round to the nearest (500th) whole number. For example, if the value in "field4" is 3655 then I want it to automatically round it to 3500.

What ever the amount is, I want it to be automatically rounded to the nearest 500. For example:
500
1000
1500
2000
2500
3000 and so on

Here is what I have so far:

<html>

<script type="text/javascript">
function startCalc(){
interval = setInterval("calc()",1);
}
function calc(){
qty = document.myform.qty.value;
document.myform.field1.value = (qty * 500);
document.myform.field2.value = (qty * 20);

amt = document.myform.field3.value;
document.myform.field4.value = (amt * 9.5);
}
function stopCalc(){
clearInterval(interval);
}
</script>

<body>

<form name="myform" method="post" action="go" onsubmit="return check(this)" />

<input type="text" name="qty" value="" onfocus="startCalc();" onblur="stopCalc();" />

<input type="text" name="field1" /><br />

<input type="text" name="field2" />
<!--end first calculations-->

<input type="text" name="field3" value="530" onfocus="startCalc();" onblur="stopCalc();" />
<input type="text" name="field4" value="" />

<input type="button" value="submit" />

</form>

</body>
</html>

Hope all of this makes sense. Thanks!

Regards,

Sara

mcjwb
07-18-2007, 10:02 PM
If you want a function that will round to the nearest 500 you can use the modulus operator %, like this:
function roundToNearest500(){
var test = document.getElementById("test");
origVal = document.myform.field10.value;
remainder = origVal % 500;
if(remainder>=250){
newVal = (origVal - remainder)+500;
}
else{
newVal = origVal - remainder;
}
document.myform.field10.value = newVal;
}
I'm not too sure what you're trying to acheive with the other fields, but I'm sure you can adapt the above to reference the right fields.

glenngv
07-18-2007, 10:09 PM
If the number is 3750, which number you want to round it to, 3500 or 4000?

user55
07-18-2007, 10:41 PM
Hi Glenn,

If its 3750, it would round to 4000. Since I want to round to the nearest 500, anything 250 and over would round up and anything below 250 would round down; since 250 is half of 500. Hope this makes sense. Thanks.

Regards,

Sara

user55
07-18-2007, 11:20 PM
Hi,

Thanks! That really helped a lot. Now, the only problem is that I can't get it to automatically stay like that. The field that its being used on displays an automated calculated value from another field. I added the

onFocus="roundToNearest500();" to the field.

It will round it off when clicked on. But when I click on another field it goes back to how it was. Is there a way to automatically display the rounded number without having to click on it and for it to stay as a rounded number when clicking on another field?

Part of the problem, is probably the onFocus event. But thats the only way I got it to work. Thanks.

Sara

mcjwb
07-18-2007, 11:37 PM
I would round the value before I put it in the field, or do you need to have it on click or something?

<html>
<script type="text/javascript">
function startCalc(){
interval = setInterval("calc()",100);
}
function calc(){
qty = document.myform.qty.value;
document.myform.field1.value = (qty * 500);
document.myform.field2.value = (qty * 20);
amt = document.myform.field3.value;
document.myform.field4.value = roundToNearest500(amt * 9.5);
}
function stopCalc(){
clearInterval(interval);
}
function roundToNearest500(origVal){
remainder = origVal % 500;
if(remainder>=250){
newVal = (origVal - remainder)+500;
}
else{
newVal = origVal - remainder;
}
return newVal;
}
</script>
<body>
<form name="myform" method="post" action="go" onsubmit="return check(this)" />

<input type="text" name="qty" value="" onfocus="startCalc();" onblur="stopCalc();" />
<input type="text" name="field1" /><br />
<input type="text" name="field2" />
<!--end first calculations-->
<input type="text" name="field3" value="530" onfocus="startCalc();" onblur="stopCalc();" />
<input type="text" name="field4" value="" />
<input type="button" value="submit" />
</form>

</body>
</html>

user55
07-19-2007, 12:38 AM
Perfect! Thanks alot!