Link to home
Start Free TrialLog in
Avatar of doctorbill
doctorbillFlag for United Kingdom of Great Britain and Northern Ireland

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=""/>

Open in new window


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
Avatar of David S.
David S.
Flag of United States of America image

ASKER CERTIFIED SOLUTION
Avatar of Zakaria Acharki
Zakaria Acharki
Flag of Morocco image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of doctorbill

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
All perfect - thanks very much for such a fast response
You're welcome, Glad we could help.
Thanks All
All answers appreciated and all work