doctorbill
asked on
Javascript to Sum Decimal Numbers
<script type="text/javascript">
function addNumbers()
{
var val1 = parseInt(document.getElementById("timepicker").value);
var val2 = parseInt(document.getElementById("timepicker2").value);
var ansD = document.getElementById("answer");
ansD.value = val2 - val1;
}
</script>
<input type="button" name="Sumbit" value="Click here to calculate Time Spent" onclick="javascript:addNumbers()"/>
Answer = <input type="text" id="answer" name="answer" value=""/>
The above code works for me when I use integers but how do I set it to be able to use decimal numbers as inputs from the textboxes and give the result as decimal
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Works perfectly. Just one other thing - how do I get the result to be xx.xx as opposed to the following:
8.45 - 4.0 = 4.449999999999999
8.45 - 4.0 = 4.449999999999999
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
All perfect - thanks very much for such a fast response
You're welcome, Glad we could help.
ASKER
Thanks All
All answers appreciated and all work
All answers appreciated and all work
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat