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 1 of 1
  1. #1
    New Coder
    Join Date
    May 2009
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Unhappy toFixed() with getElementById?

    Hi, I just started learning JavaScript, so please bear with me.

    I made a JS function that finds all the parts of a triangle. However, after calculating, the answers would be off maybe a billionth. I want to make a function that rounds the answers to 6 decimal places, but it's not working... Here's what I tried:
    Code:
    function round(){
    var x = document.getElementById("input_id").value;
    document.getElementById("div_id").innerHTML = x.toFixed(6)
    }
    and also
    Code:
    function round(){
    var x = document.getElementById("input_id").value;
    var y = x.toFixed(6)
    document.getElementById("div_id").innerHTML = y
    }
    neither of these work, but THIS DOES:
    Code:
    var x = document.getElementById("input_id").value.length;
    document.getElementById("div_id").innerHTML = x.toFixed(6)
    So, what am I doing wrong and why would 'document.getElementById.value' not work but 'document.getElementById.value.length' would?



    EDIT: Resolved, turned out I just needed to add parseFloat(x) before rounding.
    Last edited by CrackTheAir; 05-25-2009 at 01:23 AM. Reason: Resolved


 

Posting Permissions

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