Avatar of doctorbill
doctorbill
Flag 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
JavaScriptHTML

Avatar of undefined
Last Comment
doctorbill

8/22/2022 - Mon
David S.

ASKER CERTIFIED SOLUTION
Zakaria Acharki

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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
Julian Hansen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Zakaria Acharki

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
doctorbill

ASKER
All perfect - thanks very much for such a fast response
Your help has saved me hundreds of hours of internet surfing.
fblack61
Zakaria Acharki

You're welcome, Glad we could help.
doctorbill

ASKER
Thanks All
All answers appreciated and all work