Forums

This topic is locked

Results force to use 2 decimal places only

Posted 27 Jan 2011 13:08:04
1
has voted
27 Jan 2011 13:08:04 reinhardt ellis posted:
Hi,,, Please can you assist.. Im quite new.. and I cant get the answers to display as 2 decimal places.. think the problem comes in because it calculates it on change...

I have tried... a few things.. but my head is to dump..
please can you provide me with a simple solution...
i have tried tofixit().. and so on.. but no luck.. here is the clean code...

Ok I have tried.. this


((document.form1.elements.totalbox.value).toFixed(2)) =

(document.form1.elements.productvalue.value

* document.form1.elements.NoOfProducts.value);




and this


document.form1.elements.(totalbox.toFixed(2)).value =

(document.form1.elements.productvalue.value

* document.form1.elements.NoOfProducts.value);



and this...


document.form1.elements.totalbox.(value.toFixed(2)) =

(document.form1.elements.productvalue.value

* document.form1.elements.NoOfProducts.value);



And no luck.. my javascript is not that good...

Here is the original code if somebody wants to try it...


<html>

<head>



<SCRIPT language=javascript type="text/javascript">


function calculate()

{


//Box1 Start

document.form1.elements.totalbox.value =

(document.form1.elements.productvalue.value

* document.form1.elements.NoOfProducts.value);



//-------------------------------------------

document.form1.elements.totalbox2.value =

(document.form1.elements.productvalue.value

* document.form1.elements.NoOfProducts2.value);

}

</script>

<body >



<form name="form1" action="post" >
 <input type="text" name="productvalue" size="35" maxlength="100" class="blackCopy" value=""

onblur="calculate();" onKeyUp="calculate();this.blur();this.focus();" onChange="calculate();" />
 <br />
<br />
<table width="400" border="0" cellspacing="0" cellpadding="0">
 <tr>
  <td><input type="text" name="NoOfProducts" size="35" maxlength="100" class="blackCopy" value="0.07" onBlur="calculate();" onKeyUp="calculate();this.blur();this.focus();" onChange="calculate();" /></td>
  
  
  
  <td><input type="text" name ="totalbox" size="5" maxlength="5" /></td>
 </tr>
 <tr>
  <td><input type="text" name="NoOfProducts2" size="35" maxlength="100" class="blackCopy" value="0.083" onBlur="calculate();" onKeyUp="calculate();this.blur();this.focus();" onChange="calculate();" /></td>
  <td><input type="text" name ="totalbox2" size="5" maxlength="5" /></td>
 </tr>
</table>
<br />


</form>


Edited by - reinhardt ellis on 28 Jan 2011  09:27:46

Replies

Replied 31 Jan 2011 11:10:56
31 Jan 2011 11:10:56 Patrick Woldberg replied:
the toFixed() belongs on the right side:
var total = (amount * value).toFixed(2);

so in your case:
document.form1.elements.totalbox.value =
(document.form1.elements.productvalue.value *
document.form1.elements.NoOfProducts.value).toFixed(2);
Replied 03 Feb 2011 17:15:17
03 Feb 2011 17:15:17 reinhardt ellis replied:
thanx

Here is the solution for anyone that is intrested



<html>

<head>



<SCRIPT language=javascript type="text/javascript">

var x = document.form1.elements.productvalue.value * document.form1.elements.NoOfProducts.value;
document.form1.elements.totalbox.value = x.toFixed(2);


function calculate()

{


//Box1 Start

var x = document.form1.elements.productvalue.value * document.form1.elements.NoOfProducts.value;
document.form1.elements.totalbox.value = x.toFixed(2);



//-------------------------------------------

var x2 = document.form1.elements.productvalue.value * document.form1.elements.NoOfProducts2.value;
document.form1.elements.totalbox2.value = x2.toFixed(2);

}

</script>

<body onLoad="calculate();" >



<form name="form1" action="post" >
 <input type="text" name="productvalue" size="35" maxlength="100" class="blackCopy" value="100"

onblur="calculate();" onKeyUp="calculate();this.blur();this.focus();" onChange="calculate();" />
 <br />
<br />
<table width="400" border="0" cellspacing="0" cellpadding="0">
 <tr>
  <td><input type="text" name="NoOfProducts" size="35" maxlength="100" class="blackCopy" value="0.07" onBlur="calculate();" onKeyUp="calculate();this.blur();this.focus();" onChange="calculate();" /></td>
  
  
  
  <td><input type="text" name ="totalbox" size="5" maxlength="5" /></td>
 </tr>
 <tr>
  <td><input type="text" name="NoOfProducts2" size="35" maxlength="100" class="blackCopy" value="0.083" onBlur="calculate();" onKeyUp="calculate();this.blur();this.focus();" onChange="calculate();" /></td>
  <td><input type="text" name ="totalbox2" size="5" maxlength="5" /></td>
 </tr>
</table>
<br />


</form>



Reply to this topic