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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    New Coder
    Join Date
    Apr 2008
    Posts
    24
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Javascript rounding amount

    The tax amount on an order form is rounding up whereas I would like for it to show the exact amount.

    Here is the code:
    Code:
    function calculatePrice(){
     var temp = 0;
     if((document.getElementById("clientstate").value=="TX")||(document.getElementById("shippingstate").value=="TX")){
       temp = parseInt(document.getElementById("sub_hide").value) * 0.0825;
       document.getElementById("tax_hide").value=temp.toFixed(2);
     }
     else
     {
        temp = 0;
       	document.getElementById("tax_hide").value=0;
     }
     document.getElementById("total_hide").value = (parseInt(document.getElementById("sub_hide").value) + temp).toFixed(2);
     document.getElementById("tax").value ='$ '+(parseInt(document.getElementById('tax_hide').value)).toFixed(2);
     document.getElementById("total").value ='$ '+(parseInt(document.getElementById('total_hide').value)).toFixed(2);
    }
    window.onload = function(){setInterval(calculatePrice, 100);};
    If you need to see it in action here is a link to one of the products on the site: http://www.expressprintdesign.com/2008/bcards.php. Just order a business card as an example. Once you hit submit and get to the order form page, indicate under the client information that the state is Texas, which will activate the taxation on the product. You will then see that the tax rounds up on the price.

    What do I need to do to the script to fix this?

    Thanks.

  • #2
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    Try this it will round your price to two decimal places by timesing it by 100 to put the numbers you want to keep before the decimal then rounding and moving the numbers back behind the decimal.
    Code:
    var tax = Math.round(cost*0.0825*100)/100;
    var total = Math.round(cost+tax*100)/100; // returns price

  • #3
    New Coder
    Join Date
    Apr 2008
    Posts
    24
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Hi ninnypants,
    Thank you for your help. I am not the great with scripting. Where would I put this code you recommended?

  • #4
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    Use it to replace the red text:
    Code:
     if((document.getElementById("clientstate").value=="TX")||(document.getElementById("shippingstate").value=="TX")){
       temp = parseInt(document.getElementById("sub_hide").value) * 0.0825;
       document.getElementById("tax_hide").value=temp.toFixed(2);
     }
    Just make sure all of your input is going in correctly like your price

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by ninnypants View Post
    Try this it will round your price to two decimal places by timesing it by 100 to put the numbers you want to keep before the decimal then rounding and moving the numbers back behind the decimal.
    Code:
    var tax = Math.round(cost*0.0825*100)/100;
    var total = Math.round(cost+tax*100)/100; // returns price
    I guess, you don't need tax:
    Code:
    var total = Math.round(cost*108.25)/100;
    Edit: I'm wrong, tax is used in form so don't help to remove it
    regards
    Last edited by oesxyl; 08-01-2008 at 12:22 AM.

  • #6
    New Coder
    Join Date
    Apr 2008
    Posts
    24
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Hi ninnypants,
    I replaced the line as you recommended, but now the tax field does not change as it is supposed to.. ??

  • #7
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    Code:
    document.getElementById("total_hide").value = total;
    document.getElementById("tax").value ='$ '+tax;
    document.getElementById("total").value ='$ '+total;
    That should update your form fields or what I'm guessing are form fields
    Last edited by ninnypants; 08-01-2008 at 12:10 AM.

  • #8
    New Coder
    Join Date
    Apr 2008
    Posts
    24
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Do I replace some script with that or just add it under the other one?

  • #9
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    This section:
    Code:
    document.getElementById("total_hide").value = (parseInt(document.getElementById("sub_hide").value) + temp).toFixed(2);
     document.getElementById("tax").value ='$ '+(parseInt(document.getElementById('tax_hide').value)).toFixed(2);
     document.getElementById("total").value ='$ '+(parseInt(document.getElementById('total_hide').value)).toFixed(2);
    Last edited by ninnypants; 08-01-2008 at 01:13 AM. Reason: error

  • #10
    New Coder
    Join Date
    Apr 2008
    Posts
    24
    Thanks
    8
    Thanked 0 Times in 0 Posts
    I replaced that section with the other one and now the tax and the total fields come back 'undefined'

  • #11
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    Try alerting your variables at different points to see where they become undefined then we'll know where to look

  • #12
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    I was'nt able to see the page you provided a link, but basing on your script, remove all instances of toFixed():
    Code:
    function calculatePrice(){
     var temp = 0;
     if((document.getElementById("clientstate").value=="TX")||(document.getElementById("shippingstate").value=="TX")){
       temp = parseInt(document.getElementById("sub_hide").value) * 0.0825;
       document.getElementById("tax_hide").value=temp.toFixed(2);
     }
     else
     {
        temp = 0;
       	document.getElementById("tax_hide").value=0;
     }
     document.getElementById("total_hide").value = (parseInt(document.getElementById("sub_hide").value) + temp).toFixed(2);
     document.getElementById("tax").value ='$ '+(parseInt(document.getElementById('tax_hide').value)).toFixed(2);
     document.getElementById("total").value ='$ '+(parseInt(document.getElementById('total_hide').value)).toFixed(2);
    }
    Hope that does'nt cause a huge mess.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #13
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    rangana is right you also need to replace that has the toFixed() with something like where it displays the tax

  • #14
    New Coder
    Join Date
    Apr 2008
    Posts
    24
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Hi Rangana,
    I remove the toFixed instances, but that removed the decimal and 2 places after it under Tax and Total. I need it to show the decimal point with 2 places after it.

    NOTE: the funny thing is that with the original script, when the data is transferred to the database, it shows the exact amount - only on the order form is shows the rounded amount. ?!?

  • #15
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Use this:
    Code:
    <script type="text/javascript">
    function calculatePrice(){
     var temp = 0;
     if((document.getElementById("clientstate").value=="TX")||(document.getElementById("shippingstate").value=="TX")){
       temp = Number(document.getElementById("sub_hide").value) * 0.0825;
       document.getElementById("tax_hide").value=temp;
     }
     else
     {
        temp = 0;
       	document.getElementById("tax_hide").value=0;
     }
     document.getElementById("total_hide").value = (Number(document.getElementById("sub_hide").value) + temp);
     document.getElementById("tax").value ='$ '+(Number(document.getElementById('tax_hide').value));
     document.getElementById("total").value ='$ '+(Number(document.getElementById('total_hide').value));
    }
    </script>
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • Users who have thanked rangana for this post:

    FatCodeMonkey (08-01-2008)


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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