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 9 of 9
  1. #1
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post

    Round number 2 decimal points

    Hello:

    I have this js function:

    Code:
    <script type="text/javascript">
    function updatesum() {
    document.PaymentForm.newbalance.value = (document.PaymentForm.balance.value -0) - (document.PaymentForm.paymentamount.value -0);
    }
    </script>
    I want to round the value of
    Code:
    document.PaymentForm.newbalance.value
    to two decimal points.

    May I request some assistance!

  • #2
    Regular Coder
    Join Date
    Nov 2007
    Location
    Leeds, UK
    Posts
    514
    Thanks
    24
    Thanked 19 Times in 19 Posts
    toFixed(2)

    PHP Code:
    var textValue document.PaymentForm.newbalance.value;
    var 
    floatValue parseFloat(textValue);

    var 
    formatedFloat floatValue.toFixed(2
    Working towards a Internet where we don't have website just browser applications Kill the Hyper-link and say hello to 3D Games in the browser :)

  • Users who have thanked barkermn01 for this post:

    mbarandao (12-06-2010)

  • #3
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post
    Much oblige!

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    toFixed() returns a string value, so if the number is to be subject to further calculation either it must be converted back to a number with Number or parseFloat(), or the following method used to round to 2DP.

    Code:
    <script type = "text/javascript">
    
    function roundNumber(num, dec) {
    var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
    return result;
    }
    
    alert (roundNumber (12345.6789, 2));
    </script>

  • #5
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post
    Thanks Philip M for further explaining the toFixed().

    If my function that performs the calculation is this:
    Code:
    <script type="text/javascript">
    function updatesum() {
    document.PaymentForm.newbalance.value = (document.PaymentForm.balance.value -0) - (document.PaymentForm.paymentamount.value -0);
    }
    </script>
    How do I incorporate your suggested function
    Code:
    <script type = "text/javascript">
    
    function roundNumber(num, dec) {
    var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
    return result;
    }
    
    alert (roundNumber (12345.6789, 2));
    </script>
    into one to thus do the calculation and round the number?

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Code:
    <script type="text/javascript">
    function updatesum() {
    var bal = document.PaymentForm.balance.value - document.PaymentForm.paymentamount.value;
    bal = Math.round(bal*Math.pow(10,2))/Math.pow(10,2);
    document.PaymentForm.newbalance.value = bal;
    }
    </script>

  • Users who have thanked Philip M for this post:

    mbarandao (12-07-2010)

  • #7
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post
    Man, such a headache you have saved me from. Thank you sir!

    I'm quite new to js--and still learning

    Again, Thanks!
    Mossa

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Rounding 12345.00 or 12345 to 2 decimal places will result in 12345 which may not be what you want.

    You should use toFixed() for display purposes but the code given for calculation purposes. So perhaps you need both forms:-

    Code:
    <script type="text/javascript">
    function updatesum() {
    var bal = document.PaymentForm.balance.value - document.PaymentForm.paymentamount.value;
    var calcBal = Math.round(bal*Math.pow(10,2))/Math.pow(10,2);  // the value for calculations
    var displayBal = bal.toFixed(2);  // the value to be displayed
    document.PaymentForm.newbalance.value = displayBal;
    }
    </script>

  • #9
    Regular Coder
    Join Date
    Nov 2010
    Location
    Washington DC
    Posts
    338
    Thanks
    22
    Thanked 1 Time in 1 Post
    Great thought! I did in fact reproduce a similar condition during testing. This secondary suggestion perhaps addresses this.

    Again, thank you kindly!
    Mossa


  •  

    Posting Permissions

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