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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    May 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Another "how to round" question

    I have read the rounding forums but am still unsure how to incorporate it into my code.

    function calculateResult(){
    var pcintX = document.form1.x.value;
    var pcintY = document.form1.y.value;
    var pcintZ = ((pcintX * pcintX)*90.69)/(pcintY * pcintY);
    document.form1.z.value = pcintZ;
    return false;
    }

    I need the calculation to round the pcintZ value to 2 decimal places.

    Any help on how I could do this?

    Thank you

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rounding something to two digits is the same as multiply it by 100, rounding it to an integer, then dividing it by 100 again. Like this:
    Code:
    pcintZ = Math.round(pcintZ * 100) / 100;
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #3
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    If you want it to always display two decimals (instead of dropping ending zeroes), you can have a look in the FAQ. You can find a function for doing that in there.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #4
    New to the CF scene
    Join Date
    May 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Great thank you! - Problem solved.


  •  

    Posting Permissions

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