Forum Moderators: open
I have 6 fields were user can input a dollar amount, those fields I need to automatically add ".00" if the user does not.I also need those fields to be added up and that value entered into my "total" field including the ".00" if the user hasn't added an amount such as (ex 9.67) into 1 of the 6 fields. The problem that I'm getting is when the user doesn't use an amount like (ex 9.67) the "total" field doesn't give me a ".00".
example
field 1 = 6 need it to be 6.00
field 2 = 5 need it to be 5.00
total = 11 need it to be 11.00
example
field 1 = 6 need it to be 6.00
field 2 = 6.67 this is ok
total = 12.67 this is ok
I hope I made this as clear as possible..Sorry!
You can use Number.toFixed() [developer.mozilla.org] to format the result.
total.toFixed(2);
Andrew
function calc(){
one=document.getElementById('box1').value;
two=document.getElementById('box2').value;
three=document.getElementById('box3').value;
four=document.getElementById('box4').value;
five=document.getElementById('box5').value;
six=document.getElementById('box6').value;
total= (one * 1) + (two * 1) + (three * 1) + (four * 1) + (five * 1) + (six * 1);
document.getElementById('total').value= total;
document.write(total.toFixed(2));
It's the document.write that's doing that, you'd be better off assigning the result to another text field.
document.getElementById('total').value = total.toFixed(2);
Or a normal element:
document.getElementById('total').innerHTML = total.toFixed(2);
Andrew
[edit]I need to learn to type faster![/edit]
[edited by: Little_G at 6:16 pm (utc) on May 7, 2009]