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 7 of 7
  1. #1
    New Coder
    Join Date
    Jul 2007
    Posts
    62
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Auto Whole Number Help

    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]
    <html>
    <head>

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

    </head>

    <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>
    [/HTML]


    Hope all of this makes sense. Thanks!

    Regards,

    Sara
    Last edited by user55; 07-18-2007 at 05:18 PM. Reason: Update

  • #2
    Regular Coder
    Join Date
    Jul 2007
    Location
    UK
    Posts
    223
    Thanks
    0
    Thanked 14 Times in 14 Posts
    If you want a function that will round to the nearest 500 you can use the modulus operator %, like this:
    Code:
    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.

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    If the number is 3750, which number you want to round it to, 3500 or 4000?

  • #4
    New Coder
    Join Date
    Jul 2007
    Posts
    62
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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

  • #5
    New Coder
    Join Date
    Jul 2007
    Posts
    62
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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

  • #6
    Regular Coder
    Join Date
    Jul 2007
    Location
    UK
    Posts
    223
    Thanks
    0
    Thanked 14 Times in 14 Posts
    I would round the value before I put it in the field, or do you need to have it on click or something?

    How about this:
    Code:
    <html>
    <head>
    <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>
    </head>
    <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>

  • #7
    New Coder
    Join Date
    Jul 2007
    Posts
    62
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Perfect! Thanks alot!


  •  

    Posting Permissions

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