Working on a JavaScript multiplication task. The input provided is multiplied by 0.05.
The JavaScript code successfully multiplies the input number by 0.05, but encounters some issues:
- The calculated value should be rounded to two decimal points. For example: 3284.40 instead of 3284.40000000000003
- The previous output should automatically clear when a new calculation is performed.
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>
For an input value of 5688, the expected output is 3284.40, however the actual output includes additional decimal places (3284.40000000000003).
Upon clicking "Convert" multiple times, the expected output is 3284.40, but the actual output doesn't clear properly and results in incorrect values (284.40000000000003284.40000000000003)