Forum Moderators: open

Message Too Old, No Replies

adding fields + .00

adding ".00"

         

bshaffer

7:48 pm on May 6, 2009 (gmt 0)

10+ Year Member



OK! here it goes.

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!

Little_G

8:00 pm on May 6, 2009 (gmt 0)

10+ Year Member



Hi,

You can use Number.toFixed() [developer.mozilla.org] to format the result.

total.toFixed(2);

Andrew

bshaffer

6:08 pm on May 7, 2009 (gmt 0)

10+ Year Member



this doesn't seem to work and ideas? it adds the .00 but clears the entire page and puts the number at the top.

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));

LifeinAsia

6:13 pm on May 7, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Instead of
document.getElementById('total').value= total;
document.write(total.toFixed(2));
try:
document.getElementById('total').value= total.toFixed(2);

Little_G

6:14 pm on May 7, 2009 (gmt 0)

10+ Year Member



Hi,

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]