Results 1 to 5 of 5

Thread: Rounding floating results

  1. #1
    Join Date
    Nov 2009
    Location
    Maputo, Mozambique, Africa
    Posts
    14
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Rounding floating results

    Hi everybody!

    I am trying to add a rounding function inside the multiplication function Vic helped me to assemble. But is not working... I mean the multiplication works but the rounding not.
    What i need is to round the multiplication results to 2 decimal even if the number is integer (like 100 i need to be 100.00).

    The code....
    [CODE]
    <SCRIPT language = JavaScript>
    <!--
    function calculate2() {
    var frm=document.catalogo1;
    var a=frm.itemprice;
    var b=frm.itemquantity;
    var c=frm.subtotal;
    for (var z0=0;z0<a.length;z0++){
    c[z0].value=a[z0].value*b[z0].value;

    //below here is not working
    var newnumber = Math.round(c[z0].value*Math.pow(10,2))/Math.pow(10,2);
    document.catalogo1.subtotal.value = newnumber;
    }

    }
    //-->
    </SCRIPT>
    [CODE]

    What i am doing wrong?

    Alex Piotto

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <SCRIPT language = JavaScript>
    function calculate2() {
     var frm=document.catalogo1;
     var a=frm.itemprice;
     var b=frm.itemquantity;
     var c=frm.subtotal;
    // elements of the same name form an arry like collection
     for (var z0=0;z0<a.length;z0++){
      c[z0].value=Math.round(a[z0].value*b[z0].value);
     }
    
    }
    </SCRIPT>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    Nov 2009
    Location
    Maputo, Mozambique, Africa
    Posts
    14
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default

    hi Vic and thanks again... It works... you are so fast! But I need to keep the two decimals after the dot, because of the currency around here. How to do it? With math.round or...?

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Use:

    Code:
    c[z0].value=(a[z0].value*b[z0].value).toFixed(2);
    instead of:

    Code:
    c[z0].value=Math.round(a[z0].value*b[z0].value);
    But perhaps you want to round to the nearest whole amount, or more likely (as in most currency transactions where rounding is involved) round up. To simply round, and ensure two decimal places:

    Code:
    c[z0].value=Math.round(a[z0].value*b[z0].value) + '.00';
    To always round up:

    Code:
    c[z0].value=Math.ceil(a[z0].value*b[z0].value) + '.00';
    Last edited by jscheuer1; 11-09-2009 at 04:41 PM. Reason: add info
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Nov 2009
    Location
    Maputo, Mozambique, Africa
    Posts
    14
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default

    Thanks Vic and Thanks Jonh. This was helpful and I will spend a time on the code to understand how it works.
    regards
    Alex

Bookmarks

Posting Permissions

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