...

View Full Version : toFixed() with getElementById?



CrackTheAir
05-25-2009, 02:06 AM
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:


function round(){
var x = document.getElementById("input_id").value;
document.getElementById("div_id").innerHTML = x.toFixed(6)
}

and also


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:


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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum