Click here to Skip to main content
15,895,606 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how to adding three numbers and decimal in javascript

What I have tried:

JavaScript
function onlyNumbers(evt) {
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode != 46 && charCode > 31
    && (charCode < 48 || charCode > 57))
        return false;

    return true;
}

function Add(){
  var a,b,c,d;
  a=parseInt(document.getElementById("txtFirstValue").value);
  if(isNaN(a) == true){
   a = 0;
  }
  var b=parseInt(document.getElementById("txtSecondValue").value);
  if(isNaN(b) == true){
   b = 0;
  }
  var c=parseInt(document.getElementById("txtThirdValue").value);
  if(isNaN(c) == true){
   c = 0;
  }
  var d=parseInt(document.getElementById("txtFourthValue").value);
  if(isNaN(d) == true){
   d = 0;
  }
  document.getElementById("txtTotal").value=a + b + c + d;
}

HTML
    </script>


<form id="form1"  runat="server">
         <br />
        <br />
        <br />
        <center>
                <table border="0" cellpadding="1" cellspacing="1" width="400px">
                    style="border-removed black 1px solid; border-removed black 1px solid; border-removed black 1px solid; border-removed black 1px solid">
                <tr>
                    <td align="center" colspan="2" style="background-color: #ff9966" class="style2">
                    Program to add multiple values using JavaScript</td>
                </tr>
                <tr>
                    <td align="center" colspan="2">
                          
                    </td>
                </tr>
                <tr>
                    <td style="width:200px" align="right">
                        First Value:  
                    </td>
                    <td align="left" class="style1">
                        <asp:TextBox ID="txtFirstValue" runat="server" onkeypress="return onlyNumbers(event);" onKeyUp="javascript:Add();" MaxLength="10" BackColor="#FFC0C0" BorderColor="White" Font-Size="Larger" ForeColor="Black" Width="148px"></td></tr>    
                <tr>
                    <td align="right" style="width: 200px">
                        Second Value:  </td>
                    <td align="left" class="style1">
                        <asp:TextBox ID="txtSecondValue" runat="server" onkeypress="return onlyNumbers(event);" onKeyUp="javascript:Add();" MaxLength="10" BackColor="#FFC0C0" BorderColor="White" Font-Size="Larger" ForeColor="Black" Width="148px"></td>
                </tr>
                <tr>
                    <td align="right" style="width: 200px">
                        Third Value:  </td>
                    <td align="left" class="style1">
                        <asp:TextBox ID="txtThirdValue" runat="server" onkeypress="return onlyNumbers(event);" onKeyUp="javascript:Add();" MaxLength="10" BackColor="#FFC0C0" BorderColor="White" Font-Size="Larger" ForeColor="Black" Width="148px"></td>
                </tr>
                <tr>
                    <td align="right" style="width: 200px">
                        Fourth Value:  </td>
                    <td align="left" class="style1">
                        <asp:TextBox ID="txtFourthValue" runat="server" onkeypress="return onlyNumbers(event);" onKeyUp="javascript:Add();" MaxLength="10" BackColor="#FFC0C0" BorderColor="White" Font-Size="Larger" ForeColor="Black" Width="148px"></td>
                </tr>
                    <tr>
                        <td align="right" colspan="2">
                               
                        </td>
                    </tr>
                    <tr>
                        <td align="right" style="width: 200px">
                            Total =  
                        </td>
                        <td align="left" class="style1">
                            <asp:TextBox ID="txtTotal" runat="server" MaxLength="20" BackColor="#FFE0C0" Enabled="False" Font-Bold="True" Font-Size="X-Large" Width="149px"></td>
                    </tr>
                <tr>
                    <td align="right" colspan="2" id="TD2"  runat="server">
                            
                    
                </tr>
            </table>
            </center>
    </form>
Posted
Updated 27-May-16 13:08pm
v3
Comments
Peter Leow 27-May-16 6:59am    
I have two questions:
Q1. Did you write the code?
Q2. What is your question?
New_Yorker 27-May-16 7:31am    
If you want to add decimal numbers rather than integers, you need to use parseFloat instead of parseInt:

var a=parseFloat(document.getElementById("txtFirstValue").value);
var b=parseFloat(document.getElementById("txtSecondValue").value);
...
Sergey Alexandrovich Kryukov 27-May-16 9:41am    
I would add:
1) "decimal numbers" here means strings. (A number cannot be "decimal" or "hexadecimal".)
2) Why passing strings instead of numbers in first place?
—SA
Karthik_Mahalingam 27-May-16 7:56am    
Your Code works fine. what is the issue ?

Your code works fine for integer values since you are using parseInt if you want to make it work for decimal number s you will have to use parseFloat()[^] function.

just replace all the parseInt with parseFloat in your code, it should work.
JavaScript
a=parseInt(document.getElementById("txtFirstValue").value); 

JavaScript
a = parseFloat(document.getElementById("txtFirstValue").value);

similarly for all the textbox values
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-May-16 9:43am    
5ed.
—SA
Karthik_Mahalingam 27-May-16 9:47am    
Thanks Sergey
If you wish to write javaScript code you need to learn javaScript
JavaScript Tutorial[^]

Copy and paste will get you only so far - and if you don't understand what you're doing, not very far. Also, if anyone is depending upon you and you don't really know what you're doing, they're going to be hurt.

So - if you want to use a programming language and program, learn the programming language first.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900