PHP interesse |
|
Mijn javascript rekenmachine werkt goed, maar bij sommige berekeningen geeft hij verkeerd antwoorden.
- Als er bij optie1 "0.7" word gekozen x optie2 "12" dan = dat 8.4
- 0.7 x 12 = 8.4
Maar het script komt uit op 8.399999999999998
Deze site http://www.neto...isplay=0.7
Heeft dezelfde fout, als je daar 0.7 x 12 intypt komt die ook niet op 8.4 uit.
Weet iemand hoe deze fout kan opgelost worden?
<script language="javascript" type="text/javascript">
<!--
function dm(amount)
{
string = "" + amount;
dec = string.length - string.indexOf('.');
if (string.indexOf('.') == -1)
return string + '.00';
if (dec == 1)
return string + '00';
if (dec == 2)
return string + '0';
if (dec > 3)
return string.substring(0,string.length-dec+3);
return string;
}
//-->
</script>
<script language="javascript" type="text/javascript">
<!--
function calculate()
{
totaloption1 = 0.00;
if (document.form.option1_1.value > "")
{ option1_1 = document.form.option1_1.value };
document.form.option1_1.value = eval(option1_1);
if (document.form.option1_2.value > "")
{ option1_2 = document.form.option1_2.value };
document.form.option1_2.value = eval(option1_2);
totaloption1 =
eval(option1_1) *
eval(option1_2) ;
document.form.totaloption1.value = dm(eval(totaloption1));
}
//-->
</script>
<script language="javascript" type="text/javascript"> <!-- function dm(amount) { string = "" + amount; dec = string.length - string.indexOf('.'); if (string.indexOf('.') == -1) return string + '.00'; if (dec == 1) return string + '00'; if (dec == 2) return string + '0'; if (dec > 3) return string.substring(0,string.length-dec+3); return string; } //--> </script> <script language="javascript" type="text/javascript"> <!-- function calculate() { totaloption1 = 0.00; if (document.form.option1_1.value > "") { option1_1 = document.form.option1_1.value }; document.form.option1_1.value = eval(option1_1); if (document.form.option1_2.value > "") { option1_2 = document.form.option1_2.value }; document.form.option1_2.value = eval(option1_2); totaloption1 = eval(option1_1) * eval(option1_2) ; document.form.totaloption1.value = dm(eval(totaloption1)); } //--> </script>
<table border="0" cellpadding="0" width="565" id="table1">
<tr>
<td width="255" align="right">Option 1:
<select id="option1_1" name="option1_1" onchange="calculate()">
<option value="0.7">0.7</option>
<option value="1">1</option>
</select></td>
<td align="right" width="155"> </td>
</tr>
<tr>
<td width="255" align="right">Option 2:
<select id="option1_2" name="option1_2" onchange="calculate()">
<option value="0">x 0</option>
<option value="8">x 8</option>
<option value="10">x 10</option>
<option value="12">x 12</option>
<option value="14">x 14</option>
<option value="16">x 16</option>
<option value="18">x 18</option>
<option value="20">x 20</option>
<option value="22">x 22</option>
<option value="24">x 24</option>
<option value="26">x 26</option>
<option value="28">x 28</option>
<option value="30">x 30</option>
</select></td>
<td align="right" width="155"> </td>
<td align="right" width="147"><i>total €</i>
<input type="text" name="totaloption1" size="3" tabindex="99" onchange="calculate()" /></td>
</tr>
<tr>
<td align="right" width="255"><input type="submit" value="Go name="subButton" tabindex="50" /></td>
<td align="left" width="155"><input type="reset" value="Reset" name="resetButton" tabindex="50" /></td>
<td align="right" width="147"> </td>
</tr>
</table>
<table border="0" cellpadding="0" width="565" id="table1"> <td width="255" align="right">Option 1: <select id="option1_1" name="option1_1" onchange="calculate()"> <td align="right" width="155"> </td> <td width="255" align="right">Option 2: <select id="option1_2" name="option1_2" onchange="calculate()"> <td align="right" width="155"> </td> <td align="right" width="147"><i>total €</i> <input type="text" name="totaloption1" size="3" tabindex="99" onchange="calculate()" /></td> <td align="right" width="255"><input type="submit" value="Go name="subButton" tabindex="50" /></td> <td align="left" width="155"><input type="reset" value="Reset" name="resetButton" tabindex="50" /></td> <td align="right" width="147"> </td>
|