I'm doing a multiplication using JavaScript. The given input is multiplied with 0.05.
The JavaScript multiples the given input number with 0.05 but it fails in the following aspect.
- The calculated value should stop at 2 decimal points. Example: 3284.40 instead of 3284.40000000000003
- Also, the old output should clear automatically when a new value is calculated.
decimal
<script>
function doMath() {
var numOne = document.getElementById('num1').value;
var numTwo = document.getElementById('num2').value;
var theProduct = 0.05 * parseInt(numTwo);
var p = document.getElementById('theProduct');
p.innerHTML += theProduct;
document.getElmentById('doMath').innerHTML='';
}
</script>
<input id="num1" type="hidden" name="num1" value="0.05" readonly><br> Value:<br>
<input id="num2" type="text" name="num2">
<br><input type="button" value="Convert" onclick="doMath()" />
</div><div id="theProduct">$</div>
If the value given is 5688 then I expect the output to be 3284.40, but the actual output is 3284.40000000000003
When I click "Convert" twice, I expect the output to be 3284.40, but the actual output is 284.40000000000003284.40000000000003