force to use 2 decimal places only

  • Mozzi
  • Student
  • Student
  • Mozzi
  • Posts: 70

Post 3+ Months Ago

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 fixit.. and so on.. but no luck.. here is the clean code...

If anyone can please assist me with the first 2 then ill do the rest.. :) :)

Code: [ Select ]

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

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

document.form1.elements.totalbox3.value =

(document.form1.elements.productvalue.value

* document.form1.elements.NoOfProducts3.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>
 <tr>
  <td><input type="text" name="NoOfProducts3" size="35" maxlength="100" class="blackCopy" value="0.11" onBlur="calculate();" onKeyUp="calculate();this.blur();this.focus();" onChange="calculate();" /></td>
  <td><input type="text" name ="totalbox3" size="5" maxlength="5" /></td>
 </tr>
</table>
<br />


</form>
  1. <html>
  2. <head>
  3. <SCRIPT language=javascript type="text/javascript">
  4. function calculate()
  5. {
  6. //Box1 Start
  7. document.form1.elements.totalbox.value =
  8. (document.form1.elements.productvalue.value
  9. * document.form1.elements.NoOfProducts.value);
  10. //-------------------------------------------
  11. document.form1.elements.totalbox2.value =
  12. (document.form1.elements.productvalue.value
  13. * document.form1.elements.NoOfProducts2.value);
  14. //-------------------------------------------
  15. document.form1.elements.totalbox3.value =
  16. (document.form1.elements.productvalue.value
  17. * document.form1.elements.NoOfProducts3.value);
  18. }
  19. </script>
  20. <body >
  21. <form name="form1" action="post" >
  22.  <input type="text" name="productvalue" size="35" maxlength="100" class="blackCopy" value=""
  23. onblur="calculate();" onKeyUp="calculate();this.blur();this.focus();" onChange="calculate();" />
  24.  <br />
  25. <br />
  26. <table width="400" border="0" cellspacing="0" cellpadding="0">
  27.  <tr>
  28.   <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>
  29.   
  30.   
  31.   
  32.   <td><input type="text" name ="totalbox" size="5" maxlength="5" /></td>
  33.  </tr>
  34.  <tr>
  35.   <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>
  36.   <td><input type="text" name ="totalbox2" size="5" maxlength="5" /></td>
  37.  </tr>
  38.  <tr>
  39.   <td><input type="text" name="NoOfProducts3" size="35" maxlength="100" class="blackCopy" value="0.11" onBlur="calculate();" onKeyUp="calculate();this.blur();this.focus();" onChange="calculate();" /></td>
  40.   <td><input type="text" name ="totalbox3" size="5" maxlength="5" /></td>
  41.  </tr>
  42. </table>
  43. <br />
  44. </form>
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

look into the "toFixed" method.
  • Mozzi
  • Student
  • Student
  • Mozzi
  • Posts: 70

Post 3+ Months Ago

Ok I have tried.. this

Code: [ Select ]

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

(document.form1.elements.productvalue.value

* document.form1.elements.NoOfProducts.value);
  1. ((document.form1.elements.totalbox.value).toFixed(2)) =
  2. (document.form1.elements.productvalue.value
  3. * document.form1.elements.NoOfProducts.value);


and this

Code: [ Select ]

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

(document.form1.elements.productvalue.value

* document.form1.elements.NoOfProducts.value);
  1. document.form1.elements.(totalbox.toFixed(2)).value =
  2. (document.form1.elements.productvalue.value
  3. * document.form1.elements.NoOfProducts.value);


and this...

Code: [ Select ]

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

(document.form1.elements.productvalue.value

* document.form1.elements.NoOfProducts.value);
  1. document.form1.elements.totalbox.(value.toFixed(2)) =
  2. (document.form1.elements.productvalue.value
  3. * 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...

Code: [ Select ]

<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>
  1. <html>
  2. <head>
  3. <SCRIPT language=javascript type="text/javascript">
  4. function calculate()
  5. {
  6. //Box1 Start
  7. document.form1.elements.totalbox.value =
  8. (document.form1.elements.productvalue.value
  9. * document.form1.elements.NoOfProducts.value);
  10. //-------------------------------------------
  11. document.form1.elements.totalbox2.value =
  12. (document.form1.elements.productvalue.value
  13. * document.form1.elements.NoOfProducts2.value);
  14. }
  15. </script>
  16. <body >
  17. <form name="form1" action="post" >
  18. <input type="text" name="productvalue" size="35" maxlength="100" class="blackCopy" value=""
  19. onblur="calculate();" onKeyUp="calculate();this.blur();this.focus();" onChange="calculate();" />
  20. <br />
  21. <br />
  22. <table width="400" border="0" cellspacing="0" cellpadding="0">
  23. <tr>
  24.  <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>
  25.  
  26.  
  27.  
  28.  <td><input type="text" name ="totalbox" size="5" maxlength="5" /></td>
  29. </tr>
  30. <tr>
  31.  <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>
  32.  <td><input type="text" name ="totalbox2" size="5" maxlength="5" /></td>
  33. </tr>
  34. </table>
  35. <br />
  36. </form>

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 79 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.